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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Use the Process class.
käµfm³d 👽Commented:

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.
cbruneAuthor Commented:
can I make the .exe part of the whole .exe or do i have to still have it as an external .exe??
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

käµfm³d 👽Commented:
It's easier to make it external, but yes you can embed it within your executable.
cbruneAuthor Commented:
how do i embed it?  i cant let my users have access to the external .exe..
cbruneAuthor Commented:
that is my real question i guess
käµfm³d 👽Commented:
Is the embedded executable a .NET program, or a native program?
cbruneAuthor Commented:
how do i do both??
käµfm³d 👽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.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);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.