2011-01-11 119 views
0

我必須檢查遠程計算機上的進程。如果它正在運行,請不要啓動,否則請啓動遠程計算機上的進程,但問題是進程對所有用戶都不可見,所以無論何時使用下面的語法,它都會引發異常:Cannot connect to remote machine啓動前檢查遠程計算機上的進程

Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0"); 

如何通過用戶名和密碼來檢查進程是否正在運行?

+0

你有一臺機器的網絡IP地址爲169.0.0.0的? – 2011-01-11 04:43:35

回答

0

你可以嘗試冒充。

http://msdn.microsoft.com/en-us/library/ms998351.aspx

public class Test { 

[DllImport("advapi32.dll", SetLastError = true)] 
static extern bool LogonUser(string principal, string authority, string password, LogonSessionType logonType, LogonProvider logonProvider, out IntPtr token); 

[DllImport("kernel32.dll", SetLastError = true)] 
static extern bool CloseHandle(IntPtr handle); 

enum LogonSessionType : uint { 
    Interactive = 2, 
    Network, 
    Batch, 
    Service, 
    NetworkCleartext = 8, 
    NewCredentials 
} 

enum LogonProvider : uint { 
    Default = 0, 
    WinNT35, 
    WinNT40, //NTLM 
    WinNT50 //Kerb or NTLM 
} 

void DoTest() { 
    IntPtr token = IntPtr.Zero; 
    WindowsImpersonationContext user = null; 
    try { 
    bool loggedin = LogonUser("username", "authority", "password", LogonSessionType.Interactive, LogonProvider.Default, out token); 
    if (loggedin) { 
    WindowsIdentity id = new WindowsIdentity(token); 
    user = id.Impersonate(); 

    Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0"); 
    } 
    } catch (Exception ex) { 

    } finally { 
    if (user != null) { 
    user.Undo(); 
    } 
    if (token != IntPtr.Zero) { 
    CloseHandle(token); 
    } 
    } 
} 
} 

如果不工作,你可以嘗試WMI

http://www.codeproject.com/KB/cs/Remote_process_controller.aspx

相關問題