1

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?

Kieran Ojakangas
  • 495
  • 4
  • 18
  • What is your expectation? – Antoine V Jul 15 '18 at 21:08
  • Can you post the actual code? (important parts are commented out) Also did you debug to see if you actually hit the relevant code? – Dennis Kuypers Jul 15 '18 at 21:09
  • Thierry V, I was hoping to open a file in order to pass it to my program's parser to break it into different segments depending on the file format. Dennis Kuypers, I'm expecting to take a string from that file dialogue for parsing, the rest is simply logic that deals with the main window. Not sure what posting the rest of the code will accomplish as I'm dealing with a specific question and I don't want to possibly breach any proprietary agreements I may be implicitly having with this project. – Kieran Ojakangas Jul 15 '18 at 21:14
  • Dennis I didn't debug which is probably a good point there. – Kieran Ojakangas Jul 15 '18 at 21:14
  • So I found that file dialog DOES work and selects a file as soon as I step through debugging. When I hit "Continue" after selecting the file, the next window loads as expected, which I definitely find interesting. – Kieran Ojakangas Jul 15 '18 at 21:18

2 Answers2

2

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);
RezaNoei
  • 1,266
  • 1
  • 8
  • 24
  • Thank you SO much!!! I can't believe that did the trick - but hey, if Microsoft Win32 can do it without threading, I'll take it for now. – Kieran Ojakangas Jul 15 '18 at 22:15
1

OpenFileDialog.ShowDialog() is a modal method:

FileDialog is a modal dialog box; therefore, when shown, it blocks the rest of the application until the user has chosen a file. When a dialog box is displayed modally, no input (keyboard or mouse click) can occur except to objects on the dialog box. The program must hide or close the dialog box (usually in response to some user action) before input to the calling program can occur.

This means that invoking this method will block your main thread until the dialog box is closed. A few of the options you have are:

  • Invoke the FileDialog after the main window is initialized.
  • Invoke the FileDialog from a thread. In this case, be careful to synchronize.
mnistic
  • 10,866
  • 2
  • 19
  • 33
  • I totally like this answer, because it introduces an opportunity for me to consider threading in my code again. Although I'm going to mark the other answer as the right one since it's simpler, I more than appreciate your approach here - thanks! – Kieran Ojakangas Jul 15 '18 at 22:14