Search code examples
c#openfiledialog

openFileDialog DialogResult always shows NONE when opening .exe file


I am currently coding a "quickstart program" which lets you open an .exe file and start them whenever you click a button. For this I used an openFileDialog to let the user open the desired .exe files.

Additionally I extract the icons from the .exe to show it, above the start button.

This enables the user to select a file (.exe) the DialogResult of the openFileDialog shows none. It never shows OK, even though I load a normal and working .exe.

Here is my code.

private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        if (DialogResult == DialogResult.OK)
        {
            string path = openFileDialog1.FileName;
            ExtractIcon(path);
        }
    }

    private void ExtractIcon(string filePath)
    {
        Icon ico = Icon.ExtractAssociatedIcon(filePath);
            pictureBox1.Image = ico.ToBitmap();
    }

Is there any problem with my code or is it because of DialogResult?


Solution

  • Try changing the code something like this:

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
            string path = openFileDialog1.FileName;
            ExtractIcon(path);
    }