openFileDialog DialogResult always shows NONE when opening .exe file - c#

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?

Try changing the code something like this:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
ExtractIcon(path);
}

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);
}

What is DialogResult variable? Shouldn't it be:
var result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string path = openFileDialog1.FileName;
ExtractIcon(path);
}

Related

getting error in browsing files in windows using C#

I have collected a code in c# for browsing files and folders in windows. My sample code segment is as follows:
void ButtonbrowseOnClick(object obj, EventArgs ea)
{
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
}
Console.WriteLine(size); // <-- Shows file size in debugging mode.
Console.WriteLine(result); // <-- For debugging use.
}
But, I am getting the following error:
The name 'openFileDialog1' does not exist in the current context
What's wrong in the code segment?
It does not exist cause it hasn't been defined.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
Are you sure you have defined the openFileDialog1? Changing the second line of your method to the bellow, seems to solve the problem
void ButtonbrowseOnClick(object obj, EventArgs ea)
{
int size = -1;
OpenFileDialog openFileDialog1 = new OpenFileDialog(); //define the variable
DialogResult result = openFileDialog1.ShowDialog();
//your code

OpenFileDialog only select file path

I am trying to use a OpenFileDialog to let the user select a local file that can interact with my app. The file can be in use when the user selects it as I really only want to get the file's path. My issue is that if I try doing this with a OpenFileDialog, when the user presses "open", it actually tries to open the file even though I don't call for opening anywhere. Since the file is in use, it throws an error.
Is there any way for me to present a dialog for a user to just select a file an do nothing else?
Heres my current code:
DialogResult result = this.qbFilePicker.ShowDialog();
if (result == DialogResult.OK)
{
string fileName = this.qbFilePicker.FileName;
Console.WriteLine(fileName);
}
Make sure you are using
using System.Windows.Forms;
Then the following code from MSDN will not open your file unless you explicitly uncomment the bit that opens the file :)
(Take out the bits that don't pertain to you such as the .txt filter etc)
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
Console.WriteLine(fileName);
//to open the file
/*
try
{
Stream myStream = null;
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
* */
}
}

Open, select and add a .jpg file inside a form

How to open, select and add a only jpeg format file inside a form in c# from any directory of computer? Here is the code.
private void open2ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
string Chosen_File = "";
Chosen_File = openFileDialog1.FileName;
openFileDialog1.Title = "Insert an image";
openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog1.FileName ="";
openFileDialog1.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|All Files|";
openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Chosen_File = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(Chosen_File);
}
}
Just remove first openFileDialog1.ShowDialog()
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
string Chosen_File = "";
Chosen_File = openFileDialog1.FileName;
openFileDialog1.Title = "Insert an image";
openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog1.FileName = "";
openFileDialog1.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|All Files|";
//openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Chosen_File = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(Chosen_File);
}
}
Exept this, your code works well!
You have one openFileDialog1.ShowDialog(); to many. Delete the first one and use just the one inside the if clause! The if clause then uses the result after displaying the Dialog. I also prefer to explicitly checking for DialogResult.OK but that should not make a difference..
Your code shows the Dialog window twice, doesn't it? (It certainly does!) If you pick the image to display on the first dialog you will get the dialog a second time and then have to OK it there, too. If you cancel the second dialog nothing will show.
The Result i.e. the user's choice is only returned directly from the Show(); if you don't grab it there it is lost! This is different from the content of the choice, fields of the Dialog (e.g. the filename), which are all there afterwards..

How do I show the filename in listbox but keep the relative path using openfiledialog?

I am making a program in which the user needs to load in multiple files. However, in the ListBox I need to show only file names of the files they loaded but still be able to use the files loaded. So I want to hide the full path. This is how I load a file into the ListBox now, but it shows the whole path:
private void browseBttn_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
OpenFileDialog1.Filter = "DLL Files|*.dll";
OpenFileDialog1.Title = "Select a Dll File";
if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
dllList.Items.AddRange(OpenFileDialog1.FileNames);
}
}
// Set a global variable to hold all the selected files result
List<String> fullFileName;
// Browse button handler
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
OpenFileDialog1.Filter = "DLL Files|*.dll";
OpenFileDialog1.Title = "Seclect a Dll File";
if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// put the selected result in the global variable
fullFileName = new List<String>(OpenFileDialog1.FileNames);
// add just the names to the listbox
foreach (string fileName in fullFileName)
{
dllList.Items.Add(fileName.Substring(fileName.LastIndexOf(#"\")+1));
}
}
}
// handle the selected change if you wish and get the full path from the selectedIndex.
private void dllList_SelectedIndexChanged(object sender, EventArgs e)
{
// check to make sure there is a selected item
if (dllList.SelectedIndex > -1)
{
string fullPath = fullFileName[dllList.SelectedIndex];
// remove the item from the list
fullFileName.RemoveAt(dllList.SelectedIndex);
dllList.Items.Remove(dllList.SelectedItem);
}
}
You can extract the fileName of the absolute path using the static Class Path in the System.IO namespace
//returns only the filename of an absolute path.
Path.GetFileName("FilePath");

Obtain file path of C# save dialog box

I've got a save dialog box which pops up when i press a button. However i dont want to save a file at that point, i want to take the name and place it in the text box next to the button, for the name to be used later.
Can anybody tell me how to obtain the file path from the save dialog box to use it later?
Here is a sample code I just wrote very fast... instead of Console.Write you can simply store the path in a variable and use it later.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "Your extension here (*.EXT)|*.ext|All Files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(saveFileDialog1.FileName);//Do what you want here
}
Addressing the textbox...
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = saveFileDialog.FileName;
}
private void mnuFileSave_Click(object sender, EventArgs e)
{
dlgFileSave.Filter = "RTF Files|*.rtf|"+"Text files (*.txt)|*.txt|All files (*.*)|*.*";
dlgFileSave.FilterIndex = 1;
if (dlgFileSave.ShowDialog() == System.Windows.Forms.DialogResult.OK && dlgFileSave.FileName.Length > 0)
{
foreach (string strFile in dlgFileSave.FileNames)
{
SingleDocument document = new SingleDocument();
document.rtbNotice.SaveFile(strFile, RichTextBoxStreamType.RichText);
document.MdiParent = this;
document.Show();
}
}
}
Try below code.
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);

Categories