使用IP地址

时间:2016-07-11 12:24:36

标签: c#

我想在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");

2 个答案:

答案 0 :(得分:0)

这是我写的一个程序的摘录,该程序可以完成你想要做的事情。您使用了Process 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);

这也将告诉您远程进程失败的原因..如果无法启动

答案 1 :(得分:0)

你为什么要用这条线?

Process g = Process.Start(@"C:\\Program Files\RealVNC\VNC Viewer\vncviewer.exe");

所有这一切都是尝试启动本地的过程。

而是像这样初始化MagemementClass(其中server是启用WMI的计算机的路径):

process = new ManagementClass(@"\\server\root\cimv2:Win32_Process");

然后调用方法:

object[] theProcessToRun = { @"C:\\Program Files\RealVNC\VNC Viewer\vncviewer.exe" };

process.InvokeMethod("Create", theProcessToRun);