使用 IP 地址在远程计算机上打开 EXE
本文关键字:EXE 计算机 IP 地址 使用 | 更新日期: 2023-09-27 17:56:33
我想在IP为10.64.4.38的远程计算机上打开一个程序通过基于C#Windows的程序。
我要打开的文件位于 上。@"C:''Program Files''RealVNC''VNC Viewer''vncviewer.exe"
我正在使用以下代码,但它不起作用。 请帮帮我
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Default;
options.Username = userName;
options.Password = password;
options.Authority = null;
options.EnablePrivileges = true;
ManagementScope scope = new ManagementScope(path, options);
scope.Connect();
// Create the process
using (ManagementClass process = new ManagementClass("Win32_Process"))
{
ConnectionOptions conn = new ConnectionOptions();
conn.Username = "support";
conn.Password = "password";
process.Scope = scope;
process.InvokeMethod("Create", commandLine);
ManagementScope ms = new ManagementScope(@"''10.64.4.38'root'cimv2", conn);
Process g = Process.Start(@"C:''Program Files'RealVNC'VNC Viewer'vncviewer.exe");
这是我写的一个程序的摘录,它做了你想做的事情。 您使用了进程 g = .。这将创建一个本地进程。这不是你想要的。
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"''{0}'ROOT'CIMV2", remoteMachine),
connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
Console.WriteLine("{0}>Remoteexec: {1}",remoteMachine, sBatFile);
inParams["CommandLine"] = sBatFile;
inParams["CurrentDirectory"] = !nocopy?@"c:'656":@"c:'"; // this is part of my current process so this needs to be the remote folder you wish to work from, so c:'656 is just a default
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
UInt32 rv = (UInt32) outParams["returnValue"];
uint ProcessId = (uint) outParams["processId"];
String result = "";
switch (rv)
{
case 0:
result = "ok";
break;
case 2:
result = "Access denied";
break;
case 3:
result = "Insufficient privilage";
break;
case 8:
result = "Unknown failure";
break;
case 9:
result = "Path not found";
break;
case 21:
result = "Unknown failure";
break;
default:
result = "Mega bad";
break;
}
Console.WriteLine("{0}>Creation of the process returned: {1}",remoteMachine,result);
这还将告诉您远程进程失败的原因。 如果它无法启动
你为什么要用这条线?
Process g = Process.Start(@"C:''Program Files'RealVNC'VNC Viewer'vncviewer.exe");
所有这些都将尝试在本地启动该过程。
而是像这样初始化MagemementClass(其中服务器是启用WMI的计算机的路径):
process = new ManagementClass(@"''server'root'cimv2:Win32_Process");
然后调用该方法:
object[] theProcessToRun = { @"C:''Program Files'RealVNC'VNC Viewer'vncviewer.exe" };
process.InvokeMethod("Create", theProcessToRun);