tags:

views:

112

answers:

5

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

+6  A: 

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
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
@icemanind: He's using WPF, not Windows Forms - this unfortunately won't work (it was my original thought as well).
Reed Copsey
@Reed: Just updated my answer to include a WPF solution as well.
icemanind
+1  A: 

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
I get an error for StartupPath when I add this to my button event.
Joao
@Joao: What's the error you're receiving?
Reed Copsey
@Joao: I edited my answer to include the fully qualified name. Does that correct it?
Reed Copsey
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
@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
Fantastic, this worked, thanks so much.
Joao
@Joao: Glad to hear it worked. Welcome to StackOverflow! ;)
Reed Copsey
A: 

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
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 directoryAm I supposed to just insert the exe name after the = or something?
Joao
@Joao That should resolve the "But Visual c# doesn't recognize "ProcessStartInfo" at all.." though.
Rowland Shaw
A: 

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
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
A: 

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