Share via


OpenFileDialog sample

I finally got some time to dig through all my customer feedback requests and found a glaring problem! The OpenFileDialog class has basically no content on it and so I set about correcting that. It turned out there was a nice little example already living in the Dialog Boxes Overview topic but with no links to that topic from the class, it doesn't help you out much. Anyway, here is the code sample and some art showing the new updated look for the dialog boxes with .NET Framework 4 Release Candidate. Also, I want to point out that if you choose to take the time to submit feedback on the documentation (which we really, really, REALLY appreciate), not only does an actual real person read every single comment, but we also try our best to fix it. In this case, all the comments said "need sample" so if you have a sample in mind or a property or method you want to see in action, please be specific and then you will, most likely, see something show up in the content sometime in the future.

Anyway, here is the sample code and the promised image of the new dialog. This sample creates the OpenFileDialog object, open's the dialog box and shows how you can handle the result.

VB

' Configure open file dialog box
Dim dlg As 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
Dim result? As Boolean = dlg.ShowDialog()

' Process open file dialog box results
If result = True Then
    ' Open document
    Dim filename As String = dlg.FileName
End If

C#

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

And here is the image:

OpenFileDialog_Dev10_2

Margaret

Comments

  • Anonymous
    April 26, 2011
    Sweet example. I copied it into public MainWindow() and it worked - no namespace searching - no hassles. Thank you

  • Anonymous
    April 14, 2012
    Thanks you very much - perfect! Just what I was after.

  • Anonymous
    February 25, 2013
    Hi there, Thanks for the simple sample (it works!) I suggest that instead of "if result = true then" , use "if result then...." result is already a boolean variable and contains true/false already. Guy

  • Anonymous
    March 22, 2013
    I always like using this style with working with the bool? results from the new dialog types: if(dlg.ShowDialog() ?? false) {   doStuff(); } Using the null coalesce is a bit cleaner, and we only care when the value is actually true, not null or false it works very well.