3

I made a small program in c# with a button that is supposed to 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 not if I want it to run an .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 am I missing, I've tried a solution from this website:

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...

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
Joao
  • 41
  • 2
  • 3

5 Answers5

11

What your looking for is:

Application.StartupPath

It will return the startup path that your executable was started in.

If you are using WPF, try this instead:

String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • Could you give me an example usage for this? I'm terribly new to this, and I don't quite understand the microsoft website explanation. – Joao Jul 08 '10 at 16:34
  • He's using WPF, not Windows Forms - this unfortunately won't work (it was my original thought as well). – Reed Copsey Jul 08 '10 at 17:38
  • @Reed: Just updated my answer to include a WPF solution as well. – Icemanind Jul 08 '10 at 18:27
2

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);
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I get an error for StartupPath when I add this to my button event. – Joao Jul 08 '10 at 16:33
  • @Joao: I edited my answer to include the fully qualified name. Does that correct it? – Reed Copsey Jul 08 '10 at 16:50
  • I get the error after pasting your code in my event: The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) – Joao Jul 08 '10 at 17:20
  • @Joao: I missed that you're using WPF - I've updated my answer to a WPF-compatible version. This should give you what you need ;) – Reed Copsey Jul 08 '10 at 17:38
0

ProcessStartInfo is in the System.Diagnostics namespace - you need to import that namespace at the top of your cs file using a using System.Diagnostics; statement for the compiler to recognise ProcessStartInfo without specifying the namespace explicitly where you use the class.

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • Alright, I've added "using System.Diagnostics;" to the top of my page, how would my code look for this to work though? startIngo.WorkingDirectory = // working directory Am I supposed to just insert the exe name after the = or something? – Joao Jul 08 '10 at 16:40
  • @Joao That should resolve the "But Visual c# doesn't recognize "ProcessStartInfo" at all.." though. – Rowland Shaw Jul 08 '10 at 20:14
0

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

To get your local path. For example

//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")
Ishmael
  • 121
  • 2
  • 14
  • I put those 3 on my import and insert the rest of the code on my event, I get these errors: Error 1 'System.Diagnostics.Process' does not contain a definition for 'start' Error 2 'Path' is an ambiguous reference between 'System.Windows.Shapes.Path' and 'System.IO.Path' Error 3 'System.Windows.Shapes.Path' does not contain a definition for 'GetDirectoryName' Error 4 The name 'Aseembly' does not exist in the current context – Joao Jul 08 '10 at 17:24
0

There are two cases:

  1. The application was started directly - start up path can be extracted from the command-line.

  2. The application was started indirectly - e.g. from a unit-test, start up path can not be extracted from the command-line, however you can read it from the current directory into a static variable during the start-up (before the user has a chance to change it (e.g. using a file open/save dialog)).

Danny Varod
  • 17,324
  • 5
  • 69
  • 111