【问题标题】:Path not found when using InvokeMethod create使用 InvokeMethod 创建时找不到路径
【发布时间】:2014-09-12 09:00:08
【问题描述】:

当我尝试这个时得到 returnValue 9(找不到路径),我错过了什么?我正在尝试通过单击按钮运行 .bat 文件,下面的代码也不例外,但看起来找不到路径..

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();
    }

【问题讨论】:

标签: c# createprocess


【解决方案1】:

您可以使用完全限定路径作为 CommandLine [in] 参数:

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

CurrentDirectory [in] 正在设置子进程的路径,而不是 bat 文件的“路径”。

【讨论】:

  • 本地运行良好。但是当我在我的服务器上部署它时它不起作用。但我在返回值中得到 0
  • 那么问题就出在您的 .bat 文件中。权限也可能是一个问题。我假设您可以接受我的回答并打开一个新问题,您还可以在其中发布 .bat 的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 2013-10-10
  • 2016-08-27
  • 1970-01-01
  • 2016-10-31
  • 2013-09-25
相关资源
最近更新 更多