How to get permissions for using System.Diagnostics.Process.GetProcess(string)?

主宰稳场 提交于 2019-11-29 21:12:42

问题


I'm using Microsoft Visual Studio to make a simple remote task manager for experience purposes.

I want to use Process.GetProcesses(string); but there is an access denied exception that won't allow me to get the remote computer process. In fact it is normal because we should authenticate using a user name and password, but how?


回答1:


You may try to use WMI for this purpose

/// using System.Management;  
// don't forget! in VS you may have to add a new reference to this DLL
ConnectionOptions op = new ConnectionOptions();
op.Username = "REMOTE_USER";
op.Password = "REMOTE_PASSWORD";

ManagementScope sc = new ManagementScope(@"\\REMOTE_COMPUTER_NAME\root\cimv2", op);

ObjectQuery query = new ObjectQuery("Select * from Win32_Process");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(sc, query);
ManagementObjectCollection result = searcher.Get();

foreach (ManagementObject obj in result)
{
     if (obj["Caption"] != null) Console.Write(obj["Caption"].ToString() + "\t");
     if (obj["CommandLine"] != null) Console.WriteLine(obj["CommandLine"].ToString());
}

For further details on Win32_Process class see MSDN.

hth




回答2:


EDIT: Just read your post again, the steps described in my post only apply in a domain, I assume you work inside a workgroup. I'm sorry.

I recently ran into a similar issue when running Visual Studio as Administrator on Windows 7. It seems like permissions on remote machines and network shares are dropped (even in a domain!) if you elevate your program to run as local administrator, which will be the case if you run a program out of VS when VS is run as admin. This even happens if you have domain wide admin accounts.

Try the following:

  • Build the solution
  • Run it manually with your account which has hopefully privileges on the remote computer from windows explorer, without elevation

If this helps, you could stop running VS as administrator. It worked out for me.




回答3:


I'm pretty sure you need elevation to do this, or at least use a stronger user by impersonation



来源:https://stackoverflow.com/questions/6757711/how-to-get-permissions-for-using-system-diagnostics-process-getprocessstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!