-4

Using this piece of code .exe running in server

string bat =null;
bat = "D:/folder/a.bat";
System.Diagnostics.Process.Start(bat);

Error: Could not find the specified file.

Can anyone help me on this.

Oded
  • 489,969
  • 99
  • 883
  • 1,009

5 Answers5

1
  1. Make sure the file really is located at that path.
  2. Make sure your program has access to this path.
  3. Use backslashes: bat = @"D:\folder\a.bat";
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

Filepath in Windows doesn't take a forward slash, it's not a URL/URI. Use backslashes.

Anyone of below should work if the program has access to the bat file.

string bat=@"D:\folder\a.bat";

or

string bat="D:\\folder\\a.bat";

Also, checking for the existence of the bat file will be a good practice here:

if(File.Exists(bat))
{
    System.Diagnostics.Process.Start(bat);
}
0

change the slashes to backslashes:

bat = "D:\\folder\\a.bat";
daryal
  • 14,643
  • 4
  • 38
  • 54
0

Typically you need to run an executable (like cmd.exe) and then pass it a parameter. cmd.exe specifically has two options /C (carries out the command specified by the string, then terminates) and /K (carries out the command specified by the string but remains open)

David Hope
  • 2,216
  • 16
  • 32
  • BTW, as others have noted, you would need to use backslashes on the parameter for the cmd prompt. – David Hope Jan 24 '13 at 14:31
  • There is also another thread on the topic of running a bat file from a service here: http://stackoverflow.com/questions/361097/c-sharp-service-cannot-execute-batch-file – David Hope Jan 24 '13 at 14:33
0

Wrong path. Try

bat = @"D:\folder\a.bat";
Dejo
  • 2,078
  • 3
  • 26
  • 38