【问题标题】:Run a exe on remote computer placed in D drive using WMI使用 WMI 在 D 盘中的远程计算机上运行 exe
【发布时间】:2015-03-20 10:06:01
【问题描述】:

我有一个托管在 Web 服务器上的 Windows 应用程序和一个放在另一台服务器的 D 驱动器中的 exe。现在我想使用 WMI DLL 在按钮单击时从我的 Web 服务器运行该 exe。另外,我需要传递一些参数。???

我可以在远程计算机上打开记事本,但无法运行 exe。

string processPath = "path of exe"; 
var processToRun = new[] { processPath }; 
var connection = new ConnectionOptions(); 
connection.Username = 
connection.Password = 
var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", "Server Name"), connection); 
var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()); 
var result = wmiProcess.InvokeMethod("Create", processToRun);

【问题讨论】:

  • 如果有人知道请帮助我
  • 您是否收到错误消息?您确定您的应用程序没有立即启动和崩溃吗?你只有论点有问题吗?这个问题没有足够的详细信息供人们可靠地帮助您回答 - 请尝试添加更多信息。
  • 我没有提供 exe 的路径,因为当前代码会将我带到 c://windows/system32

标签: c# wmi wmi-query


【解决方案1】:

string exec = "你的 exe 名称,如果需要的话,带参数";

        System.Diagnostics.ProcessStartInfo psi =
                new System.Diagnostics.ProcessStartInfo("cmd.exe");
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardError = true;
        psi.CreateNoWindow = true;

        System.Diagnostics.Process proc =
                   System.Diagnostics.Process.Start(psi);

        System.IO.StreamReader strm = proc.StandardError;

        System.IO.StreamReader sOut = proc.StandardOutput;

        System.IO.StreamWriter sIn = proc.StandardInput;
                    sIn.WriteLine("d:");//this line moves you to the d drive  
        sIn.WriteLine("cd "+Server.MapPath("Screenshot").ToString());//here screenshot is a my directory which containing the my .exe you can chamge it with yours 
        sIn.WriteLine(exec);//here your exe runs
        strm.Close();

       sIn.WriteLine("EXIT");

       proc.Close();

       string results = sOut.ReadToEnd().Trim();

        sIn.Close();
        sOut.Close();

【讨论】:

  • 这将在我的本地机器上运行 cmd。我想在另一台服务器上运行 exe。
  • 然后使用 PsExec 之类的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多