使用WMI在放置在D驱动器中的远程计算机上运行exe

时间:2015-03-20 10:06:01

标签: c# wmi wmi-query

我有一个托管在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);

1 个答案:

答案 0 :(得分:-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();