Question

Im having some trouble my program hanging when selecting a file in a file dialog. This is the code that is showing the file browser dialog:

    private void isForgeIncluded_btn_Click(object sender, EventArgs e)
    {
        this.isForgeIncluded.Text = FolderFileDialog("file", isForgeIncluded.Text, "Forge installer file (*.jar)|*.jar");
    }

    public string FolderFileDialog(string type, string current, string fileTypes = "All files (*.*)|*.*|All files (*.*)|*.*", string StartFolder = "C:\\")
        string ReturnString = current;
        if (current != "")
        {
            StartFolder = Path.GetFullPath(current);
        }               
        if (type == "file")
        {
            OpenFileDialog minecraftFile = new OpenFileDialog();
            minecraftFile.Title = "Select file";
            minecraftFile.InitialDirectory = StartFolder;
            minecraftFile.RestoreDirectory = true;

            minecraftFile.Filter = fileTypes;
            if (minecraftFile.ShowDialog() == DialogResult.OK)
            {
                ReturnString = minecraftFile.FileName;
                return ReturnString;
            }
            minecraftFile = null;
        }
        return ReturnString; 
    }

I've narrowed the problem down to between the "if" statement and the "Return string = minecraftFile.FileName;" .. When using the debugger the program takes up to a five second break between those two lines. After its break it comes back and returns the value as if nothing was wrong. But IntelliTrace sometimes comes up with a "FileNotFound Exception" even though the program never shows me any error messages, and returns the correct value to the textbox as it should.

The wierd part is that it is not always this happens. Its random and can happen even though i select the same file as last time. The files i select are local files too on the system drive.

What could be wrong? Does the code look as it should?

Was it helpful?

Solution

Two basic problems can cause trouble with OpenFileDialog. First is misbehaving shell extension installed on your machine, they'll get loaded into your process when you open the dialog. A basic check is that you don't see trouble with Notepad's File + Open command and that you see nothing special in the Visual Studio Output window while the dialog is active. Enabling unmanaged debugging gives you more info.

The second cause is having an issue with the thread state of your UI thread. If must be initialized as a single-threaded apartment to be able to show this dialog properly. Go back to your Main() method, normally in Program.cs, and ensure it has the [STAThread] attribute.

If that doesn't pan out then we need more info about the code that's misbehaving. You need to enable unmanaged debugging, Project + Properties, Debug tab. And enable the Microsoft symbol server, Tools + Options, Debugging, Symbols. And disable Just My Code debugging, Tools + Options, Debugging, General. Hit Debug + Break All right after you get it to misbehave and post the content of the Call Stack window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top