C#跨PC远程调用程序并显示UI界面
作者:残生 时间:2022-06-22 13:42:38
在项目中有一个需求是需要在局域网内跨PC远程调用一个程序,并且要求有界面显示,调查了一些资料,能实现远程调用的.Net技术大概有PsExec、WMI、Schedule Task。
这三种方式都做了一个尝试,结果发现PsExec、WMI都只能在进程列表中看到程序执行,却无法显示界面,无法执行程序中的管理员权限操作,就连简单的在C盘创建一个txt文本都做不到。
也许是我使用方法不对,无法达到我的需求。直到后来使用了Schedule Task的方式之后,才成功实现了我的需求。
第三种技术的主要思路是先通过CMD窗口调用schtasks命令在远程PC上创建一个单次执行的计划任务,在计划任务中调用外部程序,然后立即执行,可以显示界面,并且可以执行管理员权限的操作。
用到的schtasks命令:
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 设置运行权限
需要注意的是使用这个方法远程调用程序会有相对路径方面的问题,不建议在执行程序中使用相对路径访问其他文件。
完整代码:
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();
来源:https://www.cnblogs.com/cansheng/p/16257213.html
标签:C#,远程调用,PC程序,UI
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring-Data-JPA整合MySQL和配置的方法
2023-10-29 10:19:41
![](https://img.aspxhome.com/file/2023/4/58734_0s.png)
Java深入了解数据结构之二叉搜索树增 插 删 创详解
2023-02-14 08:08:00
![](https://img.aspxhome.com/file/2023/4/60794_0s.png)
Java.try catch finally 的执行顺序说明
2022-07-06 00:30:25
![](https://img.aspxhome.com/file/2023/2/63312_0s.jpg)
Springboot文件上传功能的实现
2021-09-20 12:48:47
![](https://img.aspxhome.com/file/2023/0/64400_0s.png)
flutter实现底部导航栏
2023-08-23 01:06:13
![](https://img.aspxhome.com/file/2023/0/109150_0s.jpg)
SpringMVC一步到位精通拦截器
2023-11-25 01:47:45
![](https://img.aspxhome.com/file/2023/1/60081_0s.png)
简单谈谈java中匿名内部类构造函数
2021-09-08 18:54:10
Gson之toJson和fromJson方法的具体使用
2021-07-20 16:28:47
![](https://img.aspxhome.com/file/2023/2/75512_0s.png)
Android 消息机制问题总结
2023-08-06 03:10:49
![](https://img.aspxhome.com/file/2023/5/84715_0s.jpg)
spring循环依赖策略解析
2022-09-13 13:49:10
![](https://img.aspxhome.com/file/2023/4/72304_0s.jpg)
springboot快速集成mybatis-plus的详细教程
2021-06-11 22:05:16
![](https://img.aspxhome.com/file/2023/4/73014_0s.png)
Spring Security前后分离校验token的实现方法
2023-06-26 17:00:30
![](https://img.aspxhome.com/file/2023/5/60665_0s.png)
Java多线程的实现方式比较(两种方式比较)
2023-06-06 13:00:12
![](https://img.aspxhome.com/file/2023/8/74918_0s.jpg)
springboot配置redis过程详解
2022-10-13 12:41:30
![](https://img.aspxhome.com/file/2023/7/69887_0s.png)
浅谈java中OO的概念和设计原则(必看)
2023-11-24 13:09:56
Java Swing JList列表框的实现
2021-09-05 08:20:56
![](https://img.aspxhome.com/file/2023/4/80894_0s.gif)
深入理解Java责任链模式实现灵活的请求处理流程
2022-06-06 23:33:53
![](https://img.aspxhome.com/file/2023/7/71167_0s.png)
MyBatis整合Redis实现二级缓存的示例代码
2022-02-06 15:41:24
Java实现五子棋AI算法
2022-02-23 05:32:57
IntelliJ IDEA快速创建getter和setter方法
2023-06-04 00:44:46
![](https://img.aspxhome.com/file/2023/0/61650_0s.jpg)