How can I run an EXE file from my C# code?
using System.Diagnostics;
class Program
{
static void Main()
{
Process.Start("C:\\");
}
}
If your application needs cmd arguments, use something like this:
using System.Diagnostics;
class Program
{
static void Main()
{
LaunchCommandLineApp();
}
/// <summary>
/// Launch the application with some options set.
/// </summary>
static void LaunchCommandLineApp()
{
// For the example
const string ex1 = "C:\\";
const string ex2 = "C:\\Dir";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
}
Run an exe file from code
System.Diagnostics is included by default.
You just need to add a using (e.g. using System.Diagnostics;
) or you can declare the variable as System.Diagnostics.Process process;
code:
string exeFileAndLocation = @"C:\myConsoleApplication.exe";
string arguments = "sampleArgument";
System.Diagnostics.Process.Start(exeFileAndLocation, arguments);
How to run an exe file using C# in console application
Either include the "/c" argument
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/c w:\setup.exe";
or set FileName
directly to the setup.exe
startInfo.FileName = "w:\setup.exe";
as mentioned in the comments
Run exe file in c# within the program folder
Just use the relative path:
System.Diagnostics.Process.Start(@"Program 1.exe");
The code above will work if Program 1.exe
is in the same directory as the program running that code and the current working directory was not modified using a shortcut for example.
If not then you'll have to find out the path relative to your program (the one that should be in bin/debug/
)
How to download and run a .exe file c#
Change DownloadDataAsync
to DownloadFileAsync
.
wc.DownloadFileAsync(uri, filename);
How to include an external EXE with my code?
Normally the thirdparty executable is already installed on the system, else the exe is included in the output.
In the first case you could simply hardcode a path to Environment.SpecialFolder.ProgramFiles
or Environment.SpecialFolder.ProgramFilesX86
(32bit), or really any path you choose.
A company where i work for has control over all systems that run our software and our hierarchy is always as follows:
- Company
- Software
- App
- ThirdParty
- App1
- App2
- Software
This makes it easy to locate thirdparty software.
Another option is to just include it in the build, so it gets output to the build directory. You can do this by right clicking your project and then doing 'add existing file' and then choose any extension in the file selector popup window.
The exe then gets added as content, and you can set it to 'copy if newer' in the properties tab:
The csproj file wil look like so:
<Content Include="HmiLogViewer.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
You could also try add as link, which will add the file as link:
<Content Include="..\HmiLogViewer\HmiLogViewer\bin\Debug\HmiLogViewer.exe">
<Link>HmiLogViewer.exe</Link>
</Content>
If you want to add a directory of files, you can also add this piece of xml to the csproj:
<ItemGroup>
<DistributedFilesFolderFiles Include="..\..\DistributedFiles\**\*.*" />
</ItemGroup>
<ItemGroup>
<None Include="@(DistributedFilesFolderFiles)">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>True</Visible>
</None>
</ItemGroup>
Ofcourse adjust the path(s) to your situation.
Edit:
"That said do I need to pass the launch folder of the app as part of calling that exe file or will it automatically look there."
We look for it automatically with the option to override the value from a settings file (you could use Environment.GetCommandLineArgs
), for example when we need to test an updated version of the thirdparty exe or when the path has changed.
public static string ExecutablePathAndFileName { get; } = Process.GetCurrentProcess().MainModule.FileName;
public static string DataDrive { get; } = Path.GetPathRoot(ExecutablePathAndFileName);
public static string ExecutableFolder { get; } = Path.GetDirectoryName(ExecutablePathAndFileName);
Related Topics
Transparent Background Image for Form - Smooth Edge Shape for the Form
Should I Use Public Properties and Private Fields or Public Fields for Data
Shouldserialize*() VS *Specified Conditional Serialization Pattern
Gradient Circles for Map Generator
C# Is Rounding Down Divisions by Itself
How to Calculate Float Type Precision and Does It Make Sense
Oledbcommand Parameters Order and Priority
Weird Error Upgrading ASP.NET MVC from 4 to 5
C# Deserializing a Struct After Receiving It Through Tcp
Adding Multiple Parameterized Variables to a Database in C#
Webdriverwait Is Not Waiting for the Element I Specify
What Static Analysis Tools Are Available for C#
What Are the True Benefits of Expandoobject
Best Practice to Return Errors in ASP.NET Web API
Do You Have to Put Task.Run in a Method to Make It Async
Get Mime Type from Filename Extension
Single Controller with Multiple Get Methods in ASP.NET Web API