How to execute process on remote machine, in C#

DJPB picture DJPB · Feb 26, 2010 · Viewed 59.8k times · Source

How can I start a process on a remote computer in c#, say computer name = "someComputer", using System.Diagnostics.Process class?

I created a small console app on that remote computer that just writes "Hello world" to a txt file, and I would like to call it remotely.

Console app path: c:\MyAppFolder\MyApp.exe

Currently I have this:

ProcessStartInfo startInfo = new ProcessStartInfo(string.Format(@"\\{0}\{1}", someComputer, somePath);

            startInfo.UserName = "MyUserName";
            SecureString sec = new SecureString();
            string pwd = "MyPassword";
            foreach (char item in pwd)
            {
                sec.AppendChar(item);
            }
            sec.MakeReadOnly();
            startInfo.Password = sec;
            startInfo.UseShellExecute = false;

            Process.Start(startInfo);

I keep getting "Network path was not found".

Answer

Ivan G. picture Ivan G. · Feb 26, 2010

Can can use PsExec from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Or WMI:

object theProcessToRun() = { "YourFileHere" };

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

theClass.InvokeMethod("Create", theProcessToRun);