Reputation: 1069
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
Reputation: 64
Maybe can be useful:
if (open.ShowDialog() == Convert.ToBoolean(DialogResult.OK))
{
...
}
Upvotes: 0
Reputation: 966
Try changing the code something like this:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
ExtractIcon(path);
}
Upvotes: 8
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
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