user3827350
user3827350

Reputation: 3

Executing Batch File From Shortcut

I am trying to execute a batch file from a shortcut application on my desktop. The batch file lives on my C:drive which is where the actual application.exe is.

The problem is the CMD is executing the batch from C:\Users\hap\Desctop> and not from the executable path so it obviously cannot find my .exe file that the batch file is looking for.

Here is what I am using to execute the batch file:

System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\batch_file.bat").WaitForExit();

Upvotes: 0

Views: 189

Answers (1)

Jashaszun
Jashaszun

Reputation: 9270

What you have to do is create a ProcessStartInfo structure and set its WorkingDirectory appropriately.

You should do the following:

string workingDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
ProcessStartInfo info = new ProcessStartInfo()
{
    FileName = workingDir + "\\batch_file.bat",
    WorkingDirectory = workingDir // or wherever else you want it to execute from
};
Process p = new Process() { StartInfo = info };
p.Start();
p.WaitForExit();

Upvotes: 1

Related Questions