使用C#跨PC 远程调用程序并显示UI界面

在项目中有一个需求是需要在局域网内跨PC远程调用一个程序,并且要求有界面显示,调查了一些资料,能实现远程调用的.Net技术大概有PsExec、WMI、Schedule Task。

这三种方式都做了一个尝试,结果发现PsExec、WMI都只能在进程列表中看到程序执行,却无法显示界面,无法执行程序中的管理员权限操作,就连简单的在C盘创建一个txt文本都做不到。

也许是我使用方法不对,无法达到我的需求。直到后来使用了Schedule Task的方式之后,才成功实现了我的需求。

第三种技术的主要思路是先通过CMD窗口调用schtasks命令在远程PC上创建一个单次执行的计划任务,在计划任务中调用外部程序,然后立即执行,可以显示界面,并且可以执行管理员权限的操作。

用到的schtasks命令:

1

2

3

4

5

6

7

string queryTaskArg = string.Format(@" /query /s {0} -u domainname\{1} -p {2} /tn install", ip, username, password);

string creatTaskArg = string.Format(@" /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 = string.Format(@" /run /s {0} -u domainname\{1} -p {2} /tn install", ip, username, password); ;

string deleteTaskArg = string.Format(@" /delete /s {0} -u domainname\{1} -p {2} /tn install /F", ip, username, password);

schtasks /create  创建计划任务 

schtasks /query  查询计划任务

schtasks /run  z执行计划任务  

schtasks /delete 删除计划任务 

ip:远程PC的IP地址 

username:远程PC的登陆用户名

password:远程PC的登陆密码

/tn 计划任务的名字 /tr调用程序的路径 /sc设置执行频率 /rl 设置运行权限

需要注意的是使用这个方法远程调用程序会有相对路径方面的问题,不建议在执行程序中使用相对路径访问其他文件。

https://www.zhihu.com/collection/798877458
https://www.zhihu.com/collection/798876584
https://www.zhihu.com/collection/798876727
https://www.zhihu.com/collection/798876852
https://www.zhihu.com/collection/798877008
https://www.zhihu.com/collection/798877082
https://www.zhihu.com/collection/798877155
https://www.zhihu.com/collection/798877238
https://www.zhihu.com/collection/798877318
https://www.zhihu.com/collection/798877396
https://www.seoxiehui.cn/author-article-373047-1.html
https://www.im286.net/thread-24338485-1.html
https://www.wailian8.net/thread-2093102-1-1.html
http://www.seo-link.cn/read-846279-1.html
http://blog.fcc.qinggl.com/page-216953.html
http://blog.fcc.qinggl.com/page-217042.html
https://www.wailian8.net/thread-2107837-1-1.html
https://www.im286.net/thread-24339950-1.html
http://www.seo-link.cn/read-848789-1.html
http://blog.itpub.net/70016990/viewspace-2888910/
http://blog.fcc.qinggl.com/page-217340.html
https://tieba.baidu.com/p/7812163552
https://qiyeweixinscrm.lofter.com/
https://www.lofter.com/lpost/749cbaff_2b5476f15
https://itbbs.pconline.com.cn/soft/54708291.html
http://blog.fcc.qinggl.com/page-217622.html
http://blog.itpub.net/70016990/viewspace-2889167/
https://www.im286.net/thread-24340541-1.html
https://www.wailian8.net/thread-2113080-1-1.html
http://www.seo-link.cn/read-853939-1.html
https://www.xinhu.cn//u/6214
http://blog.fcc.qinggl.com/page-217894.html
https://www.im286.net/thread-24341047-1.html
https://www.wailian8.net/thread-2117347-1-1.html
http://www.seo-link.cn/read-856196-1.html
http://blog.fcc.qinggl.com/page-217902.html
http://www.seo-link.cn/read-857968-1.html

完整代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

string creatTaskArg = string.Format(@" /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 = string.Format(@" /run /s {0} -u domainname\{1} -p {2} /tn installSelector", ip, username, password); ;

                string deleteTaskArg = string.Format(@" /delete /s {0} -u domainname\{1} -p {2} /tn installSelector /F", ip, username, password);

                System.Diagnostics.Process p1 = new System.Diagnostics.Process();

                p1.StartInfo.FileName = @"schtasks.exe";

                p1.StartInfo.Arguments = string.Format(@" /query /s {0} -u domainname\{1} -p {2} /tn installSelector", ip, username, password);

                p1.StartInfo.UseShellExecute = false;

                p1.StartInfo.RedirectStandardError = true;

                p1.StartInfo.RedirectStandardOutput = true;

                p1.StartInfo.CreateNoWindow = true;

                p1.Start();

                p1.WaitForExit();

                string err = p1.StandardError.ReadToEnd();

                string sop = p1.StandardOutput.ReadToEnd();

                if (!string.IsNullOrEmpty(err) && string.IsNullOrEmpty(sop))

                {

                    p1.StartInfo.Arguments = creatTaskArg;

                    p1.Start();

                    p1.WaitForExit();

                    err = p1.StandardError.ReadToEnd();

                    sop = p1.StandardOutput.ReadToEnd();

                    if (!sop.ToLower().Contains("success"))

                    {

                        throw new Exception(string.Format("Create schedule task failed on {0}", ip));

                    }

      

                }

                else

                {

                    _logger.Error(err);

                }

                p1.StartInfo.Arguments = runTaskArg;

                p1.Start();

                p1.WaitForExit();

                err = p1.StandardError.ReadToEnd();

                sop = p1.StandardOutput.ReadToEnd();

                if (!string.IsNullOrEmpty(err) || !sop.ToLower().Contains("success"))

                {

                    throw new Exception(string.Format("Run schedule task failed on {0}", ip));

                }

                p1.StartInfo.Arguments = deleteTaskArg;

                p1.Start();

                p1.WaitForExit();

                err = p1.StandardError.ReadToEnd();

                sop = p1.StandardOutput.ReadToEnd();

                if (!string.IsNullOrEmpty(err) || !sop.ToLower().Contains("success"))

                {

                    throw new Exception(string.Format("Delete schedule task failed on {0}", ip));

                }

                p1.Close();