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
393 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 75

Expert Comment

by:käµfm³d 👽
ID: 37755575
Use the Process class.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37755580
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
ID: 37755592
can I make the .exe part of the whole .exe or do i have to still have it as an external .exe??
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37755599
It's easier to make it external, but yes you can embed it within your executable.
0
 

Author Comment

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

Author Comment

by:cbrune
ID: 37755609
that is my real question i guess
0
 
LVL 75

Expert Comment

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

Author Comment

by:cbrune
ID: 37755649
how do i do both??
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37755767
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

807 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