Hi, i want open an .exe file using OpenFileDialog() in C#
The code i ve used is -
Stream strm;
StreamReader reader;
try
{
strm = OpenFileDialog1.OpenFile();
reader = new StreamReader(strm);
}
catch (Exception err)
{
string Message = err.Message + "\n\nCannot open "
+ OpenFileDialog1.FileName;
MessageBox.Show(Message, "Open error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
reader.ReadToEnd();
reader.Close();
strm.Close();
But, I m getting an error..
Can anybody tell me the cause....
Thanx in advance
Loading
AlanPosted Feb 3, 2008, 9:53 AM
In that case I'm baffled as the code looks OK :-/
Just to see whether a file name is actually being returned, can you place the following line before the call to OpenFile():
MessageBox.Show(OpenFileDialog1.FileName);
If a file name does show up, then you could try using File.Open() to open it rather than OpenFileDialog1.OpenFile():
strm = File.Open(OpenFileDialog1.FileName, FileMode.Open);
p cPosted Feb 2, 2008, 12:54 AM
The code is generating eror even if i try to open .txt file...
AlanPosted Feb 1, 2008, 5:27 AM
I assume you're checking that the DialogResult property is OK higher up in the code. Otherwise, if someone pressed Cancel, there would be no selected file(s) in the OpenFileDialog to open.
Assuming you are then, as there are no arrays present in the code, the exception must be bubbling up from the code internal to the OpenFile() method or FileName property.
As you are attempting to open an .exe file, the security system may be preventing it from being opened by removing it from the array of selected filenames. This would then have a zero length which might account for the error.
Does the code work OK if you try to open a .txt file rather than an .exe?
p cPosted Feb 1, 2008, 1:18 AM
Ryan AlfordPosted Jan 31, 2008, 9:15 AM