How to open a folder with it selected? - c#

I download and use CommonOpenFileDialog from NuGet to open the folder selection window.
Since the functions are generally similar, you can think of using OpenFileDialog.
I saved the path of the folder that opened last in a text file and loaded it at the next run to run the folder selection window through CommonOpenFileDialog.Initial Directory.
It works well so far, but what I want to is that from the moment the folder selection window opens, the folder is already selected.
Since I have to check before opening the folder, it is possible to implement it right away without a folder selection window through the path, but it is an unwanted way.
I would appreciate it if you could give me advice.
I upload a part of the code I wrote for reference.
Thank you.
string readTexts = null;
if (fi.Exists)
readTexts = File.ReadAllText(fi.FullName);
folder_dialog.InitialDirectory = readTexts;
if (folder_dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
OpenTextBox.Text = selected_path = folder_dialog.FileName;
WritePath(fi, System.IO.Path.GetDirectoryName(selected_path));
}

Related

Limit a folder browser dialog to only folders containing specific files

I have a folder browser dialog (simple enough) but I only want it to be 'ok' if the folder contains files of a certain extension. What I have so far:
FolderBrowserDialog pDlg = new FolderBrowserDialog();
if (pDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (Directory.GetFiles(pDlg.SelectedPath, "*.ext").Length > 0)
{
SrcDir.Text = pDlg.SelectedPath;
}
else
{
DBox.Items.Insert(0, "Not a suitable folder");
}
}
This works in limiting the population of the textbox SrcDir if the selected folder does not contain any of the right sort of files (*.ext). It would be preferable if I could disable the 'ok' button when the folder is selected in the dialog if GetFiles(..).Length == 0 so an unsuitable folder just cannot be selected; as you cannot 'see' the files in the FolderBrowserDialog it's kind of hard for the user to know if it is the right folder, so by changing the enabled state of the OK button would indicate to the user if the folder is suitable.
I could use a OpenFileDialog to browse for one file in the directory and then use FileInfo.DirectoryName to extract the folder that its in, but I am under pressure not to do it that way (others think it's sloppy).
I am fairly sure this can't be done with a standard FolderBrowserDialog; is there another in-built dialog class that I can control this behavior or should I create a new form with a DirectorySearcher or similar TreeView and .ShowDialog() on a custom dialog form?

How can i add to menustrip a recent files menu like in visual studio: File > Recent Files?

And when you move the mouse over Recent Files it will show open a list of recent opened files ?
private void recentFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "your file name";
item.Click += new EventHandler(yourEventHandler);
recentFilesToolStripMenuItem.DropDownItems.Add(item);
}
This is my Recent Files menu click event.
But i'm not sure how to make it to remember the recent opened files even if i close my program and opend it again.
And how to make that when i move the mouse over the Recent Files menu it will open the list.
Just create a simple txt file in your root application folder named RecentFiles.txt.
Whenever you open a new file add it to that list with its complete path.
When you open your application, just read the file and populate DropdownList or wherever you want to show that list.
You can also limit the recent to 5 or 10 items by a simple logic:
Read file and populate the List(you can use string type list)
Remove the last item of the List
Add current file path as the first item of List
And then save all item line by line to the file(override the file text, not append to it)
You can refer to this article as a practical example suggested by Jens Horstmann
You can use Registry key to save the destinations of the most recent files or write them to a simple text file and read them from there at application start.

Setting the root folder of folder browser dialog

Hello I'm trying to set the root folder of a folder browser dialog. The RootFolder property of the FolderBrowserDialog can be set to an element of Environment.SpecialFolder enum.
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.RootFolder = Environment.SpecialFolder.MyDocuments;
if (folderDlg.ShowDialog() == DialogResult.OK) {
...
}
Problem is i dont want to set the root folder to some default. I want it to depend on some user selection.
Ive read about setting the selectedPath property before the dialog is shown. Therefore i tried this code
folderDlg.SelectedPath = pathSelectedByUser;
where pathSelectedByUser is a string like "C:\Temp\Backup". This is almost doing it, but it only opens the Temp directory and selects the Backup directory and doesnt open the Backup directory.
Can anyone help?
Setting the SelectedPath before showing the dialog will force the selected path to be highlighted and the file tree expanded to show the selected level folder. Unfortunate the scroll will not navigate till the selection (centralizing it in the window would be expected).
As you said, the selected path will not be opened (expanded to show subfolders), but there is a workaround. If you press '+' sign it will do the job, so a SendKeys.SendWait might be used as explained in:
Why FolderBrowserDialog dialog does not scroll to selected folder?
Regards,

Ookii VistaFolderBrowserDialog and getting selected folder

I am trying to use the Ookii dialog pack to spawn the new Vista style folder selection dialog. That all works with this simple code:
VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = Properties.Settings.Default.StoreFolder;
dlg.ShowNewFolderButton = true;
dlg.ShowDialog();
However, I cannot see any way of knowing when the user has selected a folder as there are no events on this object. I could poll for changes in SelectedPath, but that seems a terribly inefficient way of doing things.
Is there some generic C# trick I have missed to enable me to know when a user has selected a folder and therefore update other fields appropriately?
Try
VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = Properties.Settings.Default.StoreFolder;
dlg.ShowNewFolderButton = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = dlg.SelectedPath;
}
Simple answer from 2023 for WPF for future people who hate sifting through stackoverflows endless incorrect answers and needless fluff.
This answer uses Ookii.Dialogs.WPF NuGet package you can install from right inside visual studio. At the top left click Project -> Manage NuGet Packages... -> Go to browse tab and search OOkii.Dialogs.Wpf then install it. Now this code will work. :)
You can directly copy paste this.
VistaFolderBrowserDialog FolderSelect = new VistaFolderBrowserDialog(); //This starts folder selection using Ookii.Dialogs.WPF NuGet Package
FolderSelect.Description = "Please select the folder"; //This sets a description to help remind the user what their looking for.
FolderSelect.UseDescriptionForTitle = true; //This enables the description to appear.
if ((bool)FolderSelect.ShowDialog(this)) //This triggers the folder selection screen, and if the user does not cancel out...
{
TextBoxGameFolder.Text = FolderSelect.SelectedPath; //...then this happens.
}
```

Programmatically change the icon of the executable

I am developing an application called WeatherBar. Its main functionality is based on its interaction with the Windows 7 taskbar — it changes the icon depending on the weather conditions in a specific location.
The icons I am using in the application are all stored in a compiled native resource file (.res) — I am using it instead of the embedded resource manifest for icons only. By default, I modify the Icon property of the main form to change the icons accordingly and it works fine, as long as the icon is not pinned to the taskbar. When it gets pinned, the icon in the taskbar automatically switches to the default one for the executable (with index 0 in the resource file).
After doing a little bit of research, I figured that a way to change the icon would be changing the shortcut icon (as all pinned applications are actually shortcuts stored in the user folder). But it didn't work.
I assume that I need to change the icon for the executable, and therefore use UpdateResource, but I am not entirely sure about this. My executable is not digitally signed, so it shouldn't be an issue modifying it.
What would be the way to solve this issue?
If you want to do this programatically, I would start by looking at the Portable Executable file format (Wikipedia entry). The resources section (.rsrc, see section 6.9) should contain the icon. Using this information, you can write a tool to modify the icon.
If you just want to quickly change an icon in an existing file, you might be able to hack it up in the Visual Studio resource editor. I tested this with a file by deleting the old icon and adding a new one. The .exe icon changed in Explorer to the new icon, and the new icon appeared on the Start menu when I dragged it there.
-- Edit --
Yes, I agree that using UpdateResource is a good approach. Here is an example I found of using C++ functions to do so, and a P/Invoke signature for UpdateResource and FindResource.
private void button1_Click(object sender, EventArgs e)
{
String path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
String name = "test";
Shell32.Shell shl = new Shell32.ShellClass();
// Optional code to create the shortcut
System.IO.StreamWriter sw = new System.IO.StreamWriter(path + #"\" + name + ".lnk", false);
sw.Close();
// End optional code
Shell32.Folder dir = shl.NameSpace(path);
Shell32.FolderItem itm = dir.Items().Item(name + ".lnk");
Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
// Optional code to create the shortcut
lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System)
+ #"\notepad.exe";
lnk.Description = "nobugz was here";
lnk.Arguments = #"c:\sample.txt";
lnk.WorkingDirectory = #"c:\";
// End optional code
lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System)
+ "cmd.exe", 1);
lnk.Save(null);
}
This was taken from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9e23a82c-8bed-4b96-8b9a-4c2b6136a622/
It may help.
I decided to implement a workaround - the icon will change in the thumbnail for the window (it is possible in Windows 7). If the icon is unpinned, the user can see the icon changing. In case it is pinned, the thumbnail will change according to the current weather conditions.
Seems to me like the structure of pinned icons (being a shortcut, in fact) doesn't allow dynamic icon change. If I am wrong, I am open for comments and ideas on this.

Categories

Resources