Search code examples
c#wpfopenfiledialog

Select File and Continue Program In WPF


I've been trying to follow tutorials and various Stack Overflow posts, etc. to implement an OpenFileDialog to select a file. The problem is, it seems I can't get my program to continue with the rest of the logic. Not entirely sure if it has something to do with the fact I'm trying to open the file dialog inside my main window or what. Consider the following snippet:

public MainWindow()
       {
           InitializeComponent();
           string file = "";
           // Displays an OpenFileDialog so the user can select a file.  
           OpenFileDialog openFileDialog1 = new OpenFileDialog();
           openFileDialog1.Filter = "Files|*.txt;*.out";
           openFileDialog1.Title = "Select a File";
           openFileDialog1.ShowHelp = true;

           // Show the Dialog.  
           // If the user clicked OK in the dialog and  
           // a file was selected, open it.  
           if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               //file = openFileDialog1.FileName;
               //file = openFileDialog1.OpenFile().ToString();
               //openFileDialog1.Dispose();
           }
           openFileDialog1 = null;
           Console.WriteLine("File path is: " + file);

As you can see, I've even tried setting the "Help" value to true before the dialog finishes. I've tried to select both the file name for the file string, etc. but to no avail - the program seems to simply wait after the file is selected from the dialog. Would anyone here have a suggestion for a solution?


Solution

  • Previously I had the same problem with WPF. When you are working with WPF, System.Windows.Form Namespace is not included to your Project references;

    and in the other hand actually, there are two OpenFileDialog, the first is System.Windows.Forms.OpenFileDialog (this is what you have) and the second is Microsoft.Win32.OpenFileDialog. if you want to get your code to work you must add System.Windows.Forms into your references:

    Solution Explorer -> YourProject -> References (Right Click and Add Reference...) -> Assembly -> Framework -> Find And Select System.Windows.Forms -> OK

    and the Next Solution is to use Microsoft.Win32, it's pretty easy. just add this Namespace into your code file and Change your code like this:

     string file = "";
     // Displays an OpenFileDialog so the user can select a file.  
     OpenFileDialog openFileDialog1 = new OpenFileDialog();
     openFileDialog1.Filter = "Files|*.txt;*.out";
     openFileDialog1.Title = "Select a File";
    
     // Show the Dialog.  
     // If the user clicked OK in the dialog and  
     // a file was selected, open it.  
     if (openFileDialog1.ShowDialog() == true)
     {
         file = openFileDialog1.FileName;
         //file = openFileDialog1.OpenFile().ToString();
         //openFileDialog1.Dispose();
     }
     openFileDialog1 = null;
     Console.WriteLine("File path is: " + file);