Tim Kathete Stadler
Tim Kathete Stadler

Reputation: 1069

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?

Upvotes: 1

Views: 10050

Answers (4)

ndukan
ndukan

Reputation: 64

Maybe can be useful:

if (open.ShowDialog() == Convert.ToBoolean(DialogResult.OK))
{
  ...
}

Upvotes: 0

Prashant
Prashant

Reputation: 966

Try changing the code something like this:

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

Upvotes: 8

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14581

What is DialogResult variable? Shouldn't it be:

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

Upvotes: 2

Jehof
Jehof

Reputation: 35544

You need to store the return value of ShowDialog() in a variable and then compare this to DialogResult.OK

var result = openFileDialog1.ShowDialog();

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

Upvotes: 5

Related Questions