Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 1, 2008 1:46 PM
Hello everybody,
I am new to c#, and i am making some simple application.
I am using Visual Studio 2008 - windows form app
I want to be able to include other exe to my project - embedded for example (Add Existing File -> .exe) and run it on some event.
I found out how to embed picture files - and show them like stream -
i want to be able to do something like that with exe file.
I dont want the embedded exe to run inside of my application - i want it to run outside of my app when i press some button in the main application for example.
any help will be great :)
thank you for you attention :)
All replies (11)
Wednesday, July 2, 2008 10:04 AM âś…Answered
That *should* be the right place... Does it say "access denied" when you do that? It works when I add that line of code to delete the file.
Also, where I said FileMode.CreateNew, it maybe should have been FileMode.Create (otherwise it will fail if the file already exists).
Tuesday, July 1, 2008 2:07 PM
If the executable is embedded resource in your application you can extract it at runtime and launch it using Process class. Here is an example: Bat file compiler
MCTS, CodeProject MVP 2008
Tuesday, July 1, 2008 2:40 PM
isn't there any other simple method to do it - it looks like Bat compiler - works only for .bat files.
And anyway the code looks quite difficult for rookie :P
Tuesday, July 1, 2008 2:47 PM
Bat compiler is for bat files but you can use it for executable files as well.
MCTS, CodeProject MVP 2008
Tuesday, July 1, 2008 4:10 PM | 2 votes
(1) In your application project, go to Properties/Resources.
(2) Click on Add Resource.
(3) Select "Add Existing File.."
(4) Browse to the .exe you want to embed, select it and click "Open".
(5) If you want to change the resource name:
In the Resource Editor, right-click the exe you added and select "rename" and enter an appropriate name.
In this example, I called it "MyTestExe".
(6) Where you want to extract and run the executable, add code like this:
private void button_Click(object sender, EventArgs e)
{
byte[] exeBytes = Properties.Resources.MyTestExe;
string exeToRun = @"C:\TEST\MyTestExe.exe";
using (FileStream exeFile = new FileStream(exeToRun, FileMode.CreateNew))
{
exeFile.Write(exeBytes, 0, exeBytes.Length);
}
using (Process exeProcess = Process.Start(exeToRun))
{
exeProcess.WaitForExit();
}
}
Tuesday, July 1, 2008 8:57 PM
Mattew i did everything just like u said -> in Properties i created folder Resources -> then added inside MyTestExe.exe
but then i get
Error 1 'goForIt.Properties.Resources' does not contain a definition for 'MyTestExe' M:\PROGRAMING\C#\goForIt\goForIt\Form1.cs 23 52 goForIt
:(
Tuesday, July 1, 2008 9:10 PM
yes i fixed it now Mattew it works perfectly now :)
thanks alot both Giorgi and Mattew for help :)
Tuesday, July 1, 2008 10:13 PM
I just have one more question - how can i add event for the MyTestExe.exe to be deleted after i close it?
using (Process exeProcess = Process.Start(exeToRun))
{
exeProcess.WaitForExit();
File.Delete(exeToRun); - i added it here but i see its not the right place?
}
Monday, March 8, 2010 5:38 PM
(1) In your application project, go to Properties/Resources.
(2) Click on Add Resource.
(3) Select "Add Existing File.."
(4) Browse to the .exe you want to embed, select it and click "Open".
(5) If you want to change the resource name:
In the Resource Editor, right-click the exe you added and select "rename" and enter an appropriate name.
In this example, I called it "MyTestExe".
(6) Where you want to extract and run the executable, add code like this:private void button_Click(object sender, EventArgs e)
{
byte[] exeBytes = Properties.Resources.MyTestExe;
string exeToRun = @"C:\TEST\MyTestExe.exe";using (FileStream exeFile = new FileStream(exeToRun, FileMode.CreateNew))
{
exeFile.Write(exeBytes, 0, exeBytes.Length);
}using (Process exeProcess = Process.Start(exeToRun))
{
exeProcess.WaitForExit();
}
}
hello, i know this is old thread but im looking for this answer.... i did just like the instructuions and i get the same error no definition for my exe, (i didnot change the name of my exe should i remove the extension?) and i also get the error "name Process doesnt exist in current context.",... is there anything im missing or some other code i should add? and when i type the line to see what arguments are shown after the: byte[] exeBytes = Properties.Resources. i dont get to choose the exe file it doesnt appear (oddly enough a bitmap i also embbedded appears)....
thanks.
EDIT: I added the using System.Diagnostics and now i just have the same error as the op... any help how to fix it?
Wednesday, March 11, 2015 12:01 PM
Wow Wow
Matthew and shock17
this is great method.
i also want use 3rd party exe file but never work.
but follow u guys method and now this is work.
great, once again... great.
yaya we need
File.Delete(exeToRun);
in case of next start.
thanks guys ~
Thursday, October 25, 2018 11:57 AM
(1) In your application project, go to Properties/Resources.
(2) Click on Add Resource.
(3) Select "Add Existing File.."
(4) Browse to the .exe you want to embed, select it and click "Open".
(5) If you want to change the resource name:
In the Resource Editor, right-click the exe you added and select "rename" and enter an appropriate name.
In this example, I called it "MyTestExe".
(6) Where you want to extract and run the executable, add code like this:private void button_Click(object sender, EventArgs e)
{
byte[] exeBytes = Properties.Resources.MyTestExe;
string exeToRun = @"C:\TEST\MyTestExe.exe";using (FileStream exeFile = new FileStream(exeToRun, FileMode.CreateNew))
{
exeFile.Write(exeBytes, 0, exeBytes.Length);
}using (Process exeProcess = Process.Start(exeToRun))
{
exeProcess.WaitForExit();
}
}
Hi,
Thanks, worked for me just fine!