473,769 Members | 8,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opening .exe file using OpenFileDialog( ) in C# .net

10 New Member
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(st rm);
}
catch (Exception err)
{
string Message = err.Message + "\n\nCannot open "
+ OpenFileDialog1 .FileName;
MessageBox.Show (Message, "Open error",
MessageBoxButto ns.OK,
MessageBoxIcon. Error);
return;
}
reader.ReadToEn d();
reader.Close();
strm.Close();


But, I m getting an error..
Can anybody tell me the cause....
Thanx in advance
Jan 31 '08 #1
8 7379
r035198x
13,262 MVP
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(st rm);
}
catch (Exception err)
{
string Message = err.Message + "\n\nCannot open "
+ OpenFileDialog1 .FileName;
MessageBox.Show (Message, "Open error",
MessageBoxButto ns.OK,
MessageBoxIcon. Error);
return;
}
reader.ReadToEn d();
reader.Close();
strm.Close();


But, I m getting an error..
Can anybody tell me the cause....
Thanx in advance
What is the error you are getting?
Do you want to read the bytes making up the file or do you want to run the .exe program?
Jan 31 '08 #2
dvp10
10 New Member
What is the error you are getting?
Do you want to read the bytes making up the file or do you want to run the .exe program?


I want to open the .exe file and also ro run it.
The error message is- 'Index was outside the bounds of the array'
Feb 1 '08 #3
r035198x
13,262 MVP
I want to open the .exe file and also ro run it.
The error message is- 'Index was outside the bounds of the array'
To run an executable program, you need to start a process for it.
Here's an example that I use to run the javac.exe command

Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. using System.Diagnostics;
  4.  
  5. class Commands {
  6.  
  7.     public static String compileJava (String file) {
  8.  
  9.         Process p = new Process();
  10.  
  11.             p.StartInfo.FileName = "javac";
  12.  
  13.                p.StartInfo.Arguments = file;
  14.  
  15.             p.StartInfo.UseShellExecute = false;
  16.                             p.StartInfo.RedirectStandardOutput = true;
  17.  
  18.             p.Start();
  19.  
  20.                   return p.StandardOutput.ReadToEnd();
  21.  
  22.  
  23.  
  24.  
  25.     }
  26.  
  27.  
  28.         public static void Main(String[] args) {
  29.                                     String output = Commands.compileJava("/home/Java/HelloCSharp.java");
  30.  
  31.         Console.WriteLine(output);
  32.         }
  33.  
  34.  
Feb 1 '08 #4
dvp10
10 New Member
Hi r035198x,
Thanx for reply

Actually i want to run the .exe file..so it must be opened using OpenFileDialog( )..
Also even if i try to open .txt file, its not opening...

I ve tried ur code in C# bt its not working...Can u plz send me C# code for the process..' If possible'....

Thnx in advance
Feb 1 '08 #5
r035198x
13,262 MVP
Hi r035198x,
Thanx for reply

Actually i want to run the .exe file..so it must be opened using OpenFileDialog( )..
Also even if i try to open .txt file, its not opening...

I ve tried ur code in C# bt its not working...Can u plz send me C# code for the process..' If possible'....

Thnx in advance
It doesn't matter where you get the path from. set the full path of you .exe to be the process's StartInfo.FileN ame. If you get any errors then post the code you used and the error message that you got.
Feb 1 '08 #6
Plater
7,872 Recognized Expert Expert
It should be pointed out that if the EXE is running, you probably cannot open it to read in the bytes at the same time.

I am assuming you are showing the OpenFileDialog and handling the OK/Cancel button presses correctly before jumping to that line of code.
Feb 1 '08 #7
dvp10
10 New Member
Thanx,

My problem is solved..
i used System.Diagnost ic.Process.Star t("Path")

Thanx very much
Feb 6 '08 #8
r035198x
13,262 MVP
Thanx,

My problem is solved..
i used System.Diagnost ic.Process.Star t("Path")

Thanx very much
Glad you got it to work.
Feb 6 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1595
by: Stephen LeBlanc | last post by:
I'm having an issue coding in VB.NET. I'm trying to create a program that let's a user choose 2 files, using the OpenFileDialog control:1 file exists and contains data for extraction, the other file is to store the output from doing some events in program. The problem is, when the user clicks a button, that opens the OpenFileDialog box, then types the name of the file that he'd like the output to be stored in, it gives me a 'file doesn't...
16
2207
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
5
7349
by: Scott M. Lyon | last post by:
My application (a VB.NET 2003 WinApp) currently has first an OpenFileDialog (asking for an input file to the process I'm working on), and then once the user selects that, it brings up a FolderBrowserDialog box (asking for a destination for the output files from the process to be saved). The OpenFileDialog works beautifully. The problem is, after the user selects the input file from the
17
8029
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
8
6944
by: gazza67 | last post by:
Hi, I want to do something that I thought would be simple but i cant seem to work it out, perhaps someone out there could help me. I want to browse for a file (it will be a word document), save the file name to a string and then at some later stage open that file with word. The operating system will be windows 2000 (dont know if that makes a difference or not).
6
5770
by: johnlarson | last post by:
I am using an openfiledialoge to open a dbf file in a windows app. the error I get is c:\myfile.dbf not an valid filename. here is my code private void button1_Click ( object sender , EventArgs e ) { System.Windows.Forms.DataGrid ZipCode=null; System . IO . Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog ( ); openFileDialog1 . InitialDirectory =...
2
1399
by: =?Utf-8?B?TmFt?= | last post by:
Version: .NET Framework 2.0 In my Windows Forms application, how can I open a file in its native program?. Let’s say a listbox displays a list of file names with full paths; when a user double clicks on a file name, the file opens in its native program, a Word document opens in a Word Application, an HTML file opens in IE etc. etc. Much like search result pane of Windows Explorer Search utility. Thank you
23
2480
by: =?Utf-8?B?TWlrZTE5NDI=?= | last post by:
This is an example that is supposed to work in VB http://support.microsoft.com/kb/175512/en-us After spending a couple of hours downloading and installing VB Express 2008 after someone told me it was easy, the thing doesn't work any better. These two instruction lines are not functional 3. Place a Common Dialog control on the form. 4. From the Insert menu, select Module to add a single code module to the project. There is no Common...
2
1600
by: sillybob123 | last post by:
Hello, I was hoping someone could help me with my recent problem i have 1 OpenFileDialog and i want the user to only open 1 sort of file so this is what i have If ReadCon.GetName(OFD.FileName) = "CON " Then GameSaveLoc = OFD.FileName LoadCon() Else MessageBox.Show("You cannot use this kind of file!") Save.Enabled = True
0
10219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.