|
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(); // at this point error has occured 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.. ''Index was ouside the bounds of the array'' Can anybody tell me the cause.... Thanx in advance
|
| Dvp10 Friday, February 01, 2008 6:09 AM |
If you want to execute the open file then why are you reading it?
User System.Dignostic.Process.Start("file name with full path")
|
| Prasant Swain Monday, February 04, 2008 11:19 AM |
Thanx Prasant Swain,
You r write.. there is no need to read that file if i want to run it... My problem is solved.... Thanx again
|
| Dvp10 Monday, February 04, 2008 12:02 PM |
You have missed the code to show the dialog.. show it before opening the file
openFileDialog1.ShowDialog();
|
| Prasant Swain Friday, February 01, 2008 6:27 AM |
I ve written the above code inside if block...
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
Stream strm; StreamReader reader; try { strm = OpenFileDialog1.OpenFile(); // at this point error has occured 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, still I m getting an error.. ''Index was ouside the bounds of the array'' |
| Dvp10 Friday, February 01, 2008 8:53 AM |
Hi Dvp10
There is nothing wrong in your code. Are you sure strm = OpenFileDialog1.OpenFile(); line generates the error..
|
| Prasant Swain Friday, February 01, 2008 9:09 AM |
Hi Prasant,
Thanx 4 reply..
I m sure that line is generating error... Even if i try to open .txt file, the code is generating error... what should be the reason??
|
| Dvp10 Friday, February 01, 2008 12:43 PM |
If you are certain that OpenFile is generating the error, there could be following reasons, 1. The internal variable ' FileNamesInternal' from FileDialog class is not getting set. But this sounds quite unusual. 2. There is some thing problem with the IO demand for the file it is trying to open. Did you try the code from any other machine? Let me know if the same code works in other boxes.. The following code snippet should work with out any problem..
Code Snippet
OpenFileDialog ofd = new OpenFileDialog(); ofd.DefaultExt = "*.*"; if (ofd.ShowDialog(this) == DialogResult.OK) { try { Stream strem = ofd.OpenFile();
StreamReader reader = new StreamReader(strem); string text = reader.ReadToEnd();
this.Text = text.Length.ToString(); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } } |
| Moim Hossain Friday, February 01, 2008 1:22 PM |
Thanx Moim,
But this code gives the text length .... I want to run .exe
|
| Dvp10 Monday, February 04, 2008 10:55 AM |
If you want to execute the open file then why are you reading it?
User System.Dignostic.Process.Start("file name with full path")
|
| Prasant Swain Monday, February 04, 2008 11:19 AM |
Thanx Prasant Swain,
You r write.. there is no need to read that file if i want to run it... My problem is solved.... Thanx again
|
| Dvp10 Monday, February 04, 2008 12:02 PM |