Solved

If I have an external .exe and I add it to my c# project as an Add Existing Item, how do i Run the .exe from the project?

Posted on 2012-03-22
9
383 Views
Last Modified: 2012-08-13
If I have an external .exe and I add it to my c# project as an Add Existing Item, how do i Run the .exe from the project?
0
Comment
Question by:cbrune
  • 5
  • 4
9 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Use the Process class.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
P.S.

Make sure you set the item's "Copy to Output Directory" to one of the options which copies the executable to the build directory; otherwise you'll have to pass the full path to the executable to the Process class.
0
 

Author Comment

by:cbrune
Comment Utility
can I make the .exe part of the whole .exe or do i have to still have it as an external .exe??
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
It's easier to make it external, but yes you can embed it within your executable.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:cbrune
Comment Utility
how do i embed it?  i cant let my users have access to the external .exe..
0
 

Author Comment

by:cbrune
Comment Utility
that is my real question i guess
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Is the embedded executable a .NET program, or a native program?
0
 

Author Comment

by:cbrune
Comment Utility
how do i do both??
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
If it's a .NET executable, then you can use Assembly.Load to load the executable and execute it. For example:

System.IO.Stream notepad =
    System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectsNamespace.ExecutableName.exe");
System.IO.BinaryReader br = new System.IO.BinaryReader(notepad);
byte[] fileBytes = br.ReadBytes((int)notepad.Length);
System.Reflection.Assembly loadedAssembly = System.Reflection.Assembly.Load(fileBytes);
System.Reflection.MethodInfo entryPoint = loadedAssembly.EntryPoint;
entryPoint.Invoke(loadedAssembly, new object[] { new string[] { string.Empty } });

Open in new window


For a non-.NET executable, I think there is an Win API call to load the file directly into memory, but I'm not 100% on that, and I'll have to search for it. I do know that you could write the file out to disk and run it with the aforementioned Process class. You could write the file out to a temp filename (in the Temp directory). In this way, the name would be unique each time you wrote it out, and you could certainly delete the file when you were done with it. For example:

System.IO.Stream np = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectsNamespace.ExecutableName.exe");
string tempFile = System.IO.Path.GetTempFileName();
System.IO.BinaryReader npReader = new System.IO.BinaryReader(np);
byte[] npBytes = npReader.ReadBytes((int)np.Length);
System.IO.File.WriteAllBytes(tempFile, npBytes);
System.Diagnostics.Process npProc = System.Diagnostics.Process.Start(tempFile);
npProc.WaitForExit();
System.IO.File.Delete(tempFile);

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now