简体   繁体   中英

How to open task manager in remote desktop using C# programmatically

I am using the below code to open task manager in Remote desktop connection (mstsc). But this code opens task manager in my local machine:

System.Diagnostics.Process p = new System.Diagnostics.Process();
Process[] rmdProcess = Process.GetProcessesByName("mstsc");

int pid = rmdProcess[0].Id;
User32.SetForegroundWindow(rmdProcess[0].MainWindowHandle.ToInt32());
IntPtr hdcSr = System.Diagnostics.Process.GetProcessById(pid).MainWindowHandle; 

SendKeys.Send("^+{ESC}");

You need to specify the machine name. Plus you want taskmgr not mstsc .

Change this:

Process[] rmdProcess = Process.GetProcessesByName("mstsc");

...to this:

Process[] rmdProcess = Process.GetProcessesByName("taskmgr", "<insert-machine-name-here");

More

I used the following code and it worked:

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + tasklist /s <RemoteMachineName>/u <username>/p <password>);
        Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
       procStartInfo.UseShellExecute = false;
       procStartInfo.CreateNoWindow = true;
 System.Diagnostics.Process proc = new System.Diagnostics.Process();
      proc.StartInfo = procStartInfo;
      proc.Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM