Ookii VistaFolderBrowserDialog and getting selected folder - c#

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.
}
```

Related

How to open a folder with it selected?

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));
}

Disable Right click in FolderBrowserDialog dialog box - wpf?

System.Windows.Forms.FolderBrowserDialog dlg = new FolderBrowserDialog();
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
System.Windows.Forms.DialogResult result = dlg.ShowDialog(win);
I have used this to get the folder dialog box but now i need to disable right click in that folder dialog box, so that i can prevent deletion of folders from there..enter code here
Creating a custom folderDialog box is the last option i want to take..
So, Could some one suggest any possible solution for this without custom folderDialog.
You can't. The class cannot be inherited so you can't override any of the settings. There are no events to hook into.
So you have a couple options:
Roll Your Own
Use the file system to lock down your user environment.
Buy a third party control that has this functionality.
We opted for option 2, because the end users did not need to use "normal" windows apps/file locations on our RDP server, they just needed to run our application. The Organizational Unit (OU) they are added to applies permissions that they only had access to the folders we wanted them to have access to. They can't see any of the normal items you would see when the dialog is shown, but can create folders, save items, load items from the folders we give them permission to use.
Ravindra,
Since Delete in the ContextMenu is a windows feature, you would have to modify the registry settings.
In essence you have to modify/delete the Delete registry entry & after your code executes you must restore it.
You can find the registry entry under: HKEY_CLASSES_ROOT. (You would indeed take some time to figure out this entry).
Ex:
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
//Get key for New menu option in Context menu.
RegistryKey key = Registry.ClassesRoot.OpenSubKey(#"Directory\Background\shellex\ContextMenuHandlers\New",true);
//Set it to blank.
key.SetValue("", "");
fd.ShowDialog();
//Restore the value.
key.SetValue("", "{D969A300-E7FF-11d0-A93B-00A0C90F2719}");`

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?

Browse to a Directory and have the Path stored in a string (C#)

I'm trying to make this program in C# using WPF in Visual Studio. This is basically what it has to do.
When a button called "Browse" is clicked on the main form, it will open up a new form/window that let's the user browse to any directory that he chooses. After he selects the folder and clicks "Open" (or some other button on that form), the path of that directory, for example, "C:\temp" will be stored in a string variable so it can used later.
My first problem is, what do I write in the even handler of the "Browse" button that will open up a window that let's the user browse and select a folder? Is there a default window I can use or do I have to create a new form for it? Please note, the user has to select a folder, not a file like the default "Open" window.
Secondly, how do I reference a string variable so that it stores the path of the directory that the user selected?
The type you are looking for is the OpenFileDialog
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx
The basic usage is the following
using (FileDialog fileDialog = new OpenFileDialog()) {
if (DialogResult.Ok == fileDialog.ShowDialog()) {
string fileName = fileDialog.FileName;
...
}
}
EDIT
Comments clarified OP is looking to open a directory vs. a file. For this you need the FolderBrowseDialog
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
For choosing directory you should use FolderBrowserDialog. It's a control from WinForms. WPF doesn't have it's own.
For example:
var dialog = new FolderBrowserDialog();
var result = dialog.ShowDialog();
if (result == DialogResult.OK)
// ...
Don't forget to add reference to the System.Windows.Forms.
Simply do this on Button Click
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
folderpathTB.Text = fileDialog.FileName;
(folderpathTB is name of TextBox where I wana put the file path, OR u can assign it to a string variable too)

Problem with FolderBrowserDialog

If the dialog click Make new folder, just start editing the name just create a folder and click OK, OK dialogrezalt returns, but in the property SelectedPath he will name the folder New folder, then there is the name of the default
This happens because when we create, just edit and click OK, this property is not updated and the method ShowDialog () returns.
How fix this problem?
Thank you!
I had the same problem - if you created a new Folder with the FolderBrowseDialog, the .SelectedPath showed "xxx\NewFolder" not whatever new name the user had given.
The problem went away once I explicitly gave the command, prior to displaying the dialog,
MyFolderBrowser.ShowNewFolderButton = True
I failed to simulate the problem you are describing, I have tested it:
Create a new Form Form1 add button1 to it and in the button1.Click handler copy this code:
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.SelectedPath;
Console.WriteLine(path);//will not print new folder if the file renamed.
}
}
}
It worked as expected either by creating a new folder and press enter two times. or by creating a new folder and click ok.
Are you using a third party UI Controls, theams...
Edit: You stated:
Yes, if this sample run at windows application, it work correct. But
my application is Excel add-in. And FolderBrowserDialog work that I
write at started post
So you are using a third party "Excel add-in", When using a third party with FolderBrowserDialog or OpenFileDialog.. you may notice a strange behavior depending on the third party..
The solution for the problem you described is either by disabling ShowNewFolderButton or implement your own custom OpenFileDialog.

Categories

Resources