项目中有个需求,需要在局域网内跨PC远程调用一个程序,需要一个界面显示。查了一些资料,可以实现远程调用的.Net技术大概有PsExec、WMI、ScheduleTask。三种方法都试过了,结果PsExec和WMI都只能在进程列表中看到程序执行,而不能显示界面,不能在程序中进行管理员权限操作,甚至可以简单地在进程上创建程序C盘。一个txt文本做不到。可能是我用错了,不能满足我的需求。直到我使用了ScheduleTask的方法,我才成功的实现了我的需求。第三种技术的主要思想是通过CMD窗口调用schtasks命令在远程PC上创建一个单次执行的计划任务,在计划任务中调用一个外部程序,然后立即执行,界面可以显示,管理员权限可以执行操作。使用的schtasks命令:stringqueryTaskArg=string.Format(@"/query/s{0}-udomainname{1}-p{2}/tninstall",ip,username,password);字符串creatTaskArg=字符串。格式(@"/create/s{0}-u域名{1}-p{2}/scONCE/st10:00/tninstallSelector/tr{3}/rlHIGHEST/ruLocal/IT",ip,用户名、密码、安装路径);stringrunTaskArg=string.Format(@"/run/s{0}-u域名{1}-p{2}/tninstall",ip,用户名,密码);stringdeleteTaskArg=string.Format(@"/delete/s{0}-u域名{1}-p{2}/tninstall/F",ip,用户名,密码);schtasks/create创建定时任务schtasks/query查询定时任务schtasks/runz执行定时任务schtasks/delete删除定时任务ip:远程PC的IP地址username:远程PC的登录用户名password:登录远程PC密码/tn计划任务名称/tr调用程序路径/sc设置执行频率/rl设置运行权限。需要注意的是,使用这种方式远程调用程序会出现相对路径的问题。不建议使用相对路径访问执行程序中的其他文件。完整代码:stringcreatTaskArg=string.Format(@"/create/s{0}-udomainname{1}-p{2}/scONCE/st10:00/tninstallSelector/tr{3}/rlHIGHEST/ruLocal/IT",ip,用户名,密码,installSelectorPath);stringrunTaskArg=string.Format(@"/run/s{0}-u域名{1}-p{2}/tninstallSelector",ip,用户名,密码);;stringdeleteTaskArg=string.Format(@"/delete/s{0}-u域名{1}-p{2}/tninstallSelector/F",ip,用户名,密码);System.Diagnostics.Processp1=newSystem.Diagnostics.Process();p1.StartInfo.FileName=@"schtasks.exe";p1.StartInfo.Arguments=string.Format(@"/query/s{0}-u域名{1}-p{2}/tninstallSelector",ip,用户名,密码);p1.StartInfo.UseShellExecute=false;p1.StartInfo.RedirectStandardError=true;p1.StartInfo.RedirectStandardOutput=true;p1.StartInfo.CreateNoWindow=true;p1.开始();p1.WaitForExit();字符串错误=p1.StandardError.ReadToEnd();字符串sop=p1.StandardOutput.ReadToEnd();如果(!string.IsNullOrEmpty(err)&&string.IsNullOrEmpty(sop)){p1.StartInfo.Arguments=creatTaskArg;p1.开始();p1.WaitForExit();错误=p1.StandardError.ReadToEnd();sop=p1.StandardOutput.ReadToEnd();if(!sop.ToLower().Contains("success")){thrownewException(string.Format("Createscheduletaskfailedon{0}",ip));}}else{_logger.Error(err);}p1.StartInfo.Arguments=runTaskArg;p1.开始();p1.WaitForExit();错误=p1.StandardError.ReadToEnd();sop=p1.StandardOutput.ReadToEnd();if(!string.IsNullOrEmpty(err)||!sop.ToLower().Contains("success")){thrownewException(string.Format("Runscheduletaskfailedon{0}",ip));}p1.StartInfo.Arguments=deleteTaskArg;p1.开始();p1.WaitForExit();错误=p1.StandardError.ReadToEnd();sop=p1.StandardOutput.ReadToEnd();if(!string.IsNullOrEmpty(err)||!sop.ToLower().Contains("success")){thrownewException(string.Format("删除计划任务失败于{0}",ip));}p1.Close();以上就是C#学习教程:C#跨PC远程调用程序,显示UI界面共享的所有内容。有用,需要了解更多C#学习教程,希望大家多多关注——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
