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?

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?
cbruneAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
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
 
käµfm³d 👽Commented:
Use the Process class.
0
 
käµfm³d 👽Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
cbruneAuthor Commented:
can I make the .exe part of the whole .exe or do i have to still have it as an external .exe??
0
 
käµfm³d 👽Commented:
It's easier to make it external, but yes you can embed it within your executable.
0
 
cbruneAuthor Commented:
how do i embed it?  i cant let my users have access to the external .exe..
0
 
cbruneAuthor Commented:
that is my real question i guess
0
 
käµfm³d 👽Commented:
Is the embedded executable a .NET program, or a native program?
0
 
cbruneAuthor Commented:
how do i do both??
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.