How can I run an embedded .exe file in C# Windows Forms applications?

How can I run an embedded .exe file (installer) in C# Windows Forms applications the easiest way?

I just want to click a button and an installer should open. My .exe file's name is setup.

If I try Process.Start(setup.exe); I get an error:

The name 'setup' does not exist in the current context

And if I try

System.Diagnostics.Process.Start("setup");

it will open folder C:\Windows\System32\setup.

Upvotes: 0

Views: 3204

Answers (1)

Emmanuel DURIN
Emmanuel DURIN

Reputation: 4913

If what you expect is the easiest way, then don't embed setup.exe as a resource, but as Content.

(Add setup.exe to your project, and right click on setup.exe in Solution Explorer to edit properties, set as content, and select Copy if newer.)

Another option if setup.exe is a project of your Visual Studio solution, is to automatically copy setup.exe in the output directory of the launcher project: Add a reference to setup.exe if it belongs to the solution, so it is automatically copied every time you compile and there was a change.

Last is the code that is pretty easy - you already have it, actually:

System.Diagnostics.Process.Start("HelloWorld.exe");

If needed, you can change the current directory:

Environment.CurrentDirectory = @"c:\someSetupExeDir";

But better, you can use Path.Combine:

String fullPath = Path.Combine(directoryPath, fileName);

Upvotes: 2

Related Questions