Windows Forms Openfiledialog

Posted on  by 



There is the Windows API Code Pack. It's got a lot of shell related stuff, including the CommonOpenFileDialogclass (in the Microsoft.WindowsAPICodePack.Dialogsnamespace). This is the perfect solution - the usual open dialog with only folders displayed. Here is an example of how to use it. In Windows Forms (WinForms) applications, the OpenFileDialog Box is used to select single or multiple files from the Windows Folders or Directories.

  1. Windows.forms.openfiledialog Powershell
  2. C# Windows Forms Openfiledialog
  3. CommonDialog
  4. How To: Read Text From A File

In VS .NET, when you are selecting a folder for a project, a dialog that looks like an OpenFileDialog or SaveFileDialog is displayed, but is set up to accept only folders. Ever since I’ve seen this I’ve wanted to know how it’s done. I am aware of the FolderBrowserDialog, but I’ve never really liked that dialog. It starts too small and doesn’t let me take advantage of being able to type a path.

I’m almost certain by now there’s not a way to do this from .NET, but I’m just as curious how you do it from unmanaged code as well. Short of completely reimplementing the dialog from scratch, how do you modify the dialog to have this behavior?

Windows forms openfiledialog

I’d also like to restate that I am aware of the FolderBrowserDialog but sometimes I don’t like to use it, in addition to being genuinely curious how to configure a dialog in this manner. Telling me to just use the FolderBrowserDialog helps me maintain a consistent UI experience but doesn’t satisfy my curiosity so it won’t count as an answer.

It’s not a Vista-specific thing either; I’ve been seeing this dialog since VS .NET 2003, so it is doable in Win2k and WinXP. This is less of a “I want to know the proper way to do this” question and more of a “I have been curious about this since I first wanted to do it in VS 2003” question. I understand that Vista’s file dialog has an option to do this, but it’s been working in XP so I know they did something to get it to work. Vista-specific answers are not answers, because Vista doesn’t exist in the question context.

Update: I’m accepting Scott Wisniewski’s answer because it comes with a working sample, but I think Serge deserves credit for pointing to the dialog customization (which is admittedly nasty from .NET but it does work) and Mark Ransom for figuring out that MS probably rolled a custom dialog for this task.

Answers:

I have a dialog that I wrote called an OpenFileOrFolder dialog that allows you to open either a folder or a file.

If you set its AcceptFiles value to false, then it operates in only accept folder mode.

Answers:

There is the Windows API Code Pack. It’s got a lot of shell related stuff, including the CommonOpenFileDialog class (in the Microsoft.WindowsAPICodePack.Dialogs namespace). This is the perfect solution – the usual open dialog with only folders displayed.

Here is an example of how to use it:

Unfortunately Microsoft no longer ships this package, but several people have unofficially uploaded binaries to NuGet. One example can be found here. This package is just the shell-specific stuff. Should you need it, the same user has several other packages which offer more functionality present in the original package.

Answers:

You can use FolderBrowserDialogEx –
a re-usable derivative of the built-in FolderBrowserDialog. This one allows you to type in a path, even a UNC path. You can also browse for computers or printers with it. Works just like the built-in FBD, but … better.

(EDIT: I should have pointed out that this dialog can be set to select files or folders. )

Full Source code (one short C# module). Free. MS-Public license.

Code to use it:

Answers:

The Ookii.Dialogs package contains a managed wrapper around the new (Vista-style) folder browser dialog. It also degrades gracefully on older operating systems.

Answers:
Questions:

After hours of searching I found this answer by leetNightShade to a working solution.

There are three things I believe make this solution much better than all the others.

  1. It is simple to use.
    It only requires you include two files (which can be combined to one anyway) in your project.
  2. It falls back to the standard FolderBrowserDialog when used on XP or older systems.
  3. The author grants permission to use the code for any purpose you deem fit.

    There’s no license as such as you are free to take and do with the code what you will.

Windows.forms.openfiledialog Powershell

Download the code here.

Answers:

OK, let me try to connect the first dot 😉
Playing a little bit with Spy++ or Winspector shows that the Folder textbox in the VS Project Location is a customization of the standard dialog. It’s not the same field as the filename textbox in a standard file dialog such as the one in Notepad.

From there on, I figure, VS hides the filename and filetype textboxes/comboboxes and uses a custom dialog template to add its own part in the bottom of the dialog.

EDIT: Here’s an example of such customization and how to do it (in Win32. not .NET):

m_ofn is the OPENFILENAME struct that underlies the file dialog. Add these 2 lines:

where IDD_FILEDIALOG_IMPORTXLIFF is a custom dialog template that will be added in the bottom of the dialog. See the part in red below.
alt text http://apptranslator.com/_so/customizedfiledialog.png

In this case, the customized part is only a label + an hyperlink but it could be any dialog. It could contain an OK button that would let us validate folder only selection.

Openfiledialog

But how we would get rid of some of the controls in the standard part of the dialog, I don’t know.

More detail in this MSDN article.

Answers:

Exact Audio Copy works this way on Windows XP. The standard file open dialog is shown, but the filename field contains the text “Filename will be ignored”.

Just guessing here, but I suspect the string is injected into the combo box edit control every time a significant change is made to the dialog. As long as the field isn’t blank, and the dialog flags are set to not check the existence of the file, the dialog can be closed normally.

Edit: this is much easier than I thought. Here’s the code in C++/MFC, you can translate it to the environment of your choice.

C# Windows Forms Openfiledialog

Edit 2: This should be the translation to C#, but I’m not fluent in C# so don’t shoot me if it doesn’t work.

Edit 3: Finally looked at the actual dialog in question, in Visual Studio 2005 (I didn’t have access to it earlier). It is not the standard file open dialog! If you inspect the windows in Spy++ and compare them to a standard file open, you’ll see that the structure and class names don’t match. When you look closely, you can also spot some differences between the contents of the dialogs. My conclusion is that Microsoft completely replaced the standard dialog in Visual Studio to give it this capability. My solution or something similar will be as close as you can get, unless you’re willing to code your own from scratch.

Answers:

You can subclass the file dialog and gain access to all its controls. Each has an identifier that can be used to obtain its window handle. You can then show and hide them, get messages from them about selection changes etc. etc. It all depends how much effort you want to take.

We did ours using WTL class support and customized the file dialog to include a custom places bar and plug-in COM views.

MSDN provides information on how to do this using Win32, this CodeProject article includes an example, and this CodeProject article provides a .NET example.

Answers:

You can use code like this

  • The filter is hide files
  • The filename is hide first text

To advanced hide of textbox for filename you need to look at
OpenFileDialogEx

The code:

Answers:

CommonDialog

I assume you’re on Vista using VS2008? In that case I think that the FOS_PICKFOLDERS option is being used when calling the Vista file dialog IFileDialog. I’m afraid that in .NET code this would involve plenty of gnarly P/Invoke interop code to get working.

Answers:

Try this one from Codeproject (credit to Nitron):

I think it’s the same dialog you’re talking about – maybe it would help if you add a screenshot?

'}; if(::SHGetPathFromIDList(pIDL, buffer) != 0) { // Set the string value. folderpath = buffer; retVal = true; } // free the item id list CoTaskMemFree(pIDL); } ::OleUninitialize(); return retVal;}
Answers:

You can use code like this

The filter is empty string.
The filename is AnyName but not blank

Answers:

First Solution

I developed this as a cleaned up version of .NET Win 7-style folder select dialog by Bill Seddon of lyquidity.com (I have no affiliation). (I learned of his code from another answer on this page). I wrote my own because his solution requires an additional Reflection class that isn’t needed for this focused purpose, uses exception-based flow control, doesn’t cache the results of its reflection calls. Note that the nested static VistaDialog class is so that its static reflection variables don’t try to get populated if the Show method is never called. It falls back to the pre-Vista dialog if not in a high enough Windows version. Should work in Windows 7, 8, 9, 10 and higher (theoretically).

How To: Read Text From A File

It is used like so in a Windows Form:

You can of course play around with its options and what properties it exposes. For example, it allows multiselect in the Vista-style dialog.

Second Solution

Simon Mourier gave an answer that shows how to do the exact same job using interop against the Windows API directly, though his version would have to be supplemented to use the older style dialog if in an older version of Windows. Unfortunately, I hadn’t found his post yet when I worked up my solution. Name your poison!

Answers:

On Vista you can use IFileDialog with FOS_PICKFOLDERS option set. That will cause display of OpenFileDialog-like window where you can select folders:

For older Windows you can always resort to trick with selecting any file in folder.

Working example that works on .NET Framework 2.0 and later can be found here.

Using
Answers:

I know the question was on configuration of OpenFileDialog but seeing that Google brought me here i may as well point out that if you are ONLY looking for folders you should be using a FolderBrowserDialog Instead as answered by another SO question below

Tags: file, select





Coments are closed