Running .exe from the same folder of my C # program

I made a small program in C # with a button that should open another .exe file.

It works fine if I use:

private void start_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start(@"path to file"); } 

But no, if I want it to run .exe from the same folder, I basically wanted something like:

 private void start_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start(@"program.exe"); } 

What I miss, I tried the solution from this site:

 var startIngo = new ProcessStartInfo(); startIngo.WorkingDirectory = // working directory // set additional properties Process proc = Process.Start(startIngo); 

But Visual C # doesn't recognize "ProcessStartInfo" at all ...

+3
source share
5 answers

What are you looking for:

 Application.StartupPath 

It will return the startup path to which your executable was run.

If you are using WPF, try this instead:

 String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); 
+11
source

You can do:

 var startupPath = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetEntryAssembly().Location); var programPath = System.IO.Path.Combine(startupPath, "program.exe"); System.Diagnostics.Process.Start(programPath); 
+2
source

ProcessStartInfo is located in the System.Diagnostics namespace - you need to import this namespace at the top of your cs file using the using System.Diagnostics; statement using System.Diagnostics; for the compiler to recognize ProcessStartInfo without explicitly specifying the namespace in which you are using the class.

0
source

You can also try System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

To get your local path. for instance

 //in your imports/using section using System.IO using System.Reflection using System.Diagnostics; //in your code to execute Process.start(Path.GetDirectoryName(Aseembly.GetExecutingAssembly().GetName().CodeBase) + "\\program.exe") 
0
source

There are two cases:

  • The application was launched directly - the launch path can be extracted from the command line.

  • The application was launched indirectly - for example, from a single test, the launch path cannot be extracted from the command line, however, you can read it from the current directory into a static variable at startup (before the user can change it (for example, using the file open / save dialog )).

0
source

All Articles