0

I am unable to use the Win32 OpenFileDialog class

I tried the sample code below, which I copy-pasted straight from the Microsoft Documentation into my method, but I got error CS0246, because the compiler could not find OpenFileDialog.

I tried to add a reference to Win32, but it is nowhere to be found.

BTW, I did try to use the .NET OpenFileDialog and FolderBrowserDialog classes, but they cannot open a folder with a start location and that option is absolutely necessary to my application.

What did I do wrong ?

Here's my code.

// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dlg.FileName;
}

EDIT : PROBLEM SOLVED (solution below)

The bug came from the form designer. I initially dropped a FolderBrowserDialog object in my form. By default Visual Studio 2015 creates an object with RootFolder set to Desktop. Now, even if you set SelectedPath to your target folder, FolderBrowserDialog would still open the desktop folder instead of it.

So I instantiated a FolderBrowserDialog object inside my event handler and set SelectedPath to my target folder leaving RootFolder unset. And it now works like a charm.

private void B_Browse_Click(object sender, EventArgs e)
{
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.SelectedPath = MyTargetFolder;
        DialogResult result = fbd.ShowDialog();
        // do stuff
}    

Thanks everybody and have a good day :)

  • 3
    OpenFileDialog from the .Net library has an InitialDirectory and FolderBrowserDialog has a RootFolder both are used to set the start location of the dialog...... Also no where in your example do you set the start location of the dialog as pointed out this was necessary in your app. – Sorceri Jun 07 '16 at 16:35
  • RootFolder only takes variables of the type Environment.SpecialFolder. As to SelectedPath, it doesn't work wether you set it with RootFolder or not. BTW, I didn't add a start location in my code since it doesn't work in the first place, spent the whole day googling and trying to no avail, so I didn't go further after the compiler complained. – quaero_semper Jun 07 '16 at 16:39
  • This does not work for you https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.initialdirectory%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – paparazzo Jun 07 '16 at 18:43
  • Well, it does work for picking files, but not for folders. Apparently Microsoft had decided to only allow picking folder through the FolderBrowser class, unfortunately you cannot set a start location (many people on StackOverflow had the same issue) – quaero_semper Jun 08 '16 at 07:34

3 Answers3

1

For WinForms, you should be using System.Windows.Forms.OpenFileDialog object.

Pecheneg
  • 768
  • 3
  • 11
  • 27
  • System.Windows.Forms.OpenFileDialog doesn't let you pick a folder (or at least not without some dirty hack) – quaero_semper Jun 07 '16 at 16:43
  • My bad then. I rechecked things. – Pecheneg Jun 07 '16 at 16:45
  • That's what the [FolderBrowserDialog](https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog%28v=vs.110%29.aspx) is for. And you can set the initial directory with [RootFolder](https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.rootfolder%28v=vs.110%29.aspx). Or [SelectedPath](https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.selectedpath%28v=vs.110%29.aspx). – ManoDestra Jun 07 '16 at 16:53
1

You can set the start folder with FolderBrowseDialog, the issue is that the tree view will not scroll to it, see referenced SO Question.

Why FolderBrowserDialog dialog does not scroll to selected folder?

        FolderBrowserDialog fbd = new FolderBrowserDialog();
        fbd.RootFolder = Environment.SpecialFolder.MyComputer;
        fbd.SelectedPath = @"C:\SomeFolder\";
        fbd.ShowDialog();
Community
  • 1
  • 1
Sorceri
  • 7,870
  • 1
  • 29
  • 38
0

Make sure your using System.Windows.Forms statement is there.

Then it's really easy:

OpenFileDialog dlg = new OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";


Nullable<bool> result = dlg.ShowDialog(); 
// I get an error on this "cannot implicitly convert"
DialogResult result = dlg.ShowDialog();


if (result == true) //doesn't work with DialogResult
{
string filename = dlg.FileName;
}

Honestly there a number of things wrong with that. See this is another stack article related. Hope this helps.

Community
  • 1
  • 1
kyle_engineer
  • 280
  • 1
  • 11
  • OpenFileDialog doesn't let you pick a folder. There's a hack I found on SO, which is to set CheckFileExists and ValidateNames to false and CheckPathExists to true, but it didn't solve the problem. I had to set a filename, but users might mistakenly delete it and the be unable to select a folder. – quaero_semper Jun 08 '16 at 07:26
  • What are you trying to open? If your trying to capture directory data, there's different ways of going about that. – kyle_engineer Jun 08 '16 at 16:13
  • I was just trying to select a subfolder in a specific folder. Ideally, it should've been done in the old OpenFileDialog way. Anyway, I finally solved the issue, I'm editing my question to publish the solution in case someone else has the same issue. Thanks anyway :) – quaero_semper Jun 09 '16 at 09:21