1

Getting returnValue 9 (path not found) when I'm trying this, what am I missing? I'm trying to run a .bat file from a button click and the code bellow gives me no exception but it looks like the path can't be found..

try
    {
        ManagementClass management = new ManagementClass("Win32_Process");

        ManagementBaseObject inParams = management.GetMethodParameters("Create");

        inParams["CommandLine"] = "test.bat";
        inParams["CurrentDirectory"] = @"C:\test\"; //this is where test.bat is

        var output = management.InvokeMethod("Create", inParams, null);

        lblStatusResponse.Text = "" + output["returnValue"];
    }
    catch (Exception ex)
    {
        lblStatusResponse.Text = ex.ToString();
    }
MrProgram
  • 5,044
  • 13
  • 58
  • 98

1 Answers1

2

You can use the fully qualified path as the CommandLine [in] parameter:

inParams["CommandLine"] = @"c:\test\test.bat";

The CurrentDirectory [in] is setting the path for the child process and not the "path" to the bat file.

Postlagerkarte
  • 6,600
  • 5
  • 33
  • 52
  • It works fine local. But when I deploy this on my server it doesn't work. But I get 0 in returnvalue – MrProgram Sep 12 '14 at 12:50
  • The problem then is within your .bat file then. Permissions can be a problem too. I assume you can accept my answer and open a new question where you also post content of your .bat. – Postlagerkarte Sep 12 '14 at 13:10