One requirement in the project is that a program needs to be called remotely across the PC within the local area network, and requires an interface to be displayed. Some data have been investigated. .Net technologies that can realize remote calls are probably PsExec, WMI, and Schedule Task.
All three methods have been tried, and it turned out that PsExec and WMI can only see program execution in the process list, but cannot display the interface, cannot execute administrator rights operations in the program, and cannot even simply create a txt text on the C drive.
Maybe I use it wrong and cannot meet my needs. It was not until later that I used the Schedule Task method that I successfully realized my needs.
The main idea of the third technology is to first call the schtasks command through the CMD window to create a single-execute planned task on the remote PC, call an external program in the planned task, and then execute it immediately. The interface can be displayed and the administrator permission operations can be performed.
The schtasks command used:
string queryTaskArg = (@" /query /s {0} -u domainname\{1} -p {2} /tn install", ip, username, password); string creatTaskArg = (@" /create /s {0} -u domainname\{1} -p {2} /sc ONCE /st 10:00 /tn installSelector /tr {3} /rl HIGHEST /ru Local /IT", ip, username, password, installPath); string runTaskArg = (@" /run /s {0} -u domainname\{1} -p {2} /tn install", ip, username, password); ; string deleteTaskArg = (@" /delete /s {0} -u domainname\{1} -p {2} /tn install /F", ip, username, password);
schtasks /create Create scheduled tasks
schtasks /query query schedule tasks
schtasks /run z perform scheduled tasks
schtasks /delete Delete Schedule Tasks
ip: The IP address of the remote PC
username: the login username of the remote PC
password: the login password of the remote PC
/tn The name of the scheduled task /tr path to the caller /sc sets execution frequency /rl sets running permissions
It should be noted that using this method to remotely call the program will have problems with relative paths. It is not recommended to use relative paths to access other files in the executor.
Complete code:
string creatTaskArg = (@" /create /s {0} -u domainname\{1} -p {2} /sc ONCE /st 10:00 /tn installSelector /tr {3} /rl HIGHEST /ru Local /IT", ip, username, password, installSelectorPath); string runTaskArg = (@" /run /s {0} -u domainname\{1} -p {2} /tn installSelector", ip, username, password); ; string deleteTaskArg = (@" /delete /s {0} -u domainname\{1} -p {2} /tn installSelector /F", ip, username, password); p1 = new (); = @""; = (@" /query /s {0} -u domainname\{1} -p {2} /tn installSelector", ip, username, password); = false; = true; = true; = true; (); (); string err = (); string sop = (); if (!(err) && (sop)) { = creatTaskArg; (); (); err = (); sop = (); if (!().Contains("success")) { throw new Exception(("Create schedule task failed on {0}", ip)); } } else { _logger.Error(err); } = runTaskArg; (); (); err = (); sop = (); if (!(err) || !().Contains("success")) { throw new Exception(("Run schedule task failed on {0}", ip)); } = deleteTaskArg; (); (); err = (); sop = (); if (!(err) || !().Contains("success")) { throw new Exception(("Delete schedule task failed on {0}", ip)); } ();
The above is the detailed content of C# remotely calling the program across PCs and displaying the UI interface. For more information about C# remotely calling the UI to display the UI across PCs, please pay attention to my other related articles!