2014-03-12 34 views
0

我有一个自定义应用程序安装或其他系统&我想从此系统使用WMI C#调用该应用程序,而不使用任何批处理文件。在使用WMI的远程系统上调用应用程序(具有参数的自定义应用程序)

此外,该应用程序有命令参数运行。那么,任何人都可以引导我想要编码的东西吗?

我已经尝试了几个我在此粘贴的代码片段(代码片段)供您参考,它可以在Notepad.exe或Calc.exe运行的情况下正常工作。 实际上,它也适用于我的自定义应用程序,不带参数但不带参数。当我传递参数时,它在2秒后开始&杀人。这意味着它不会以适当的格式传递参数。

private static uint CreateProcess(ManagementScope connectionScope, string exeWithPathAndArguments) 
    { 
     try 
     { 
      var objectGetOptions = new ObjectGetOptions(); 
      ManagementPath processPath = new ManagementPath("Win32_Process"); 

      using (var processTask = new ManagementClass(connectionScope, processPath, objectGetOptions)) 
      { 
       using (var methodInParams = processTask.GetMethodParameters("Create")) 
       { 
        methodInParams["CommandLine"] = exeWithPathAndArguments; 
        using (var methodOutParams = processTask.InvokeMethod("Create", methodInParams, null)) 
        { 
         var err = (uint)methodOutParams["returnValue"]; 
         if (err != 0) 
         { 
          var info = "see http://msdn.microsoft.com/en-us/library/windows/desktop/aa389388(v=vs.85).aspx"; 
          switch (err) 
          { 
           case 2: info = "Access Denied"; break; 
           case 3: info = "Insufficient Privilege"; break; 
           case 8: info = "Unknown failure"; break; 
           case 9: info = "Path Not Found"; break; 
           case 21: info = "Invalid Parameter"; break; 
           default: info = "Unknown(Code)"; break; 
          } 

          var msg = "Failed to Start the Process, error = " + methodOutParams["returnValue"] + " (" + info + ")"; 
          throw new Exception(msg); 
         } 

         return (uint)methodOutParams["processId"]; 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

我已经意识到PSExec,但我不想使用它。同时我不想使用批处理文件来运行我的命令。只是想用直接命令传递的方式来运行应用程序。 我的应用程序位置不在PATH目录中。所以,显然我需要提供一个完整的路径,如...

CreateProcess(connectionScope,exeWithPathAndArguments.Trim()); 其中exeWithPathAndArguments将是 “/” C:\ Program Files文件(x86)的\公司\应用程序文件夹\ APP.EXE/“-argsName argvalue”

回答

0

添加methodInParams["CurrentDirectory"] = wheretostartapp;和删除-argsName

所以你exeWithPathAndArguments将是:

\"C:\\\\Program files (x86)\\\\Company\\\\Application Folder\\\\app.exe\" argvalue

相关问题