如何用C# 实现远程关机呢

来源:百度知道 编辑:UC知道 时间:2024/06/25 04:30:18
1:客户端+服务端。
2:不需要客户端。
我要c#编程 不是3389- -

两种方法,一种是服务端开一个web service端口及实现关机注销等的函数,或封装一个关机注销等的类使用.net remoting。这样客户端直接通过访问服务端的这个远程函数或远程对象,就可以实现关机注销。另一种是通过windows远程关机的dos(cmd)命令,来实现这个功能,即使用 http://topic.csdn.net/t/20050620/07/4093050.html 上所说的方法。创建一个Process,第一种是在服务端创建,第二种是客户端创建,一样的使用process和shutdown命令。
创建cmd进程,并运行命令。方法如下类似

Process commandProcess = new Process();
try
{
commandProcess.StartInfo.FileName = "cmd.exe";

commandProcess.StartInfo.UseShellExecute = false;
commandProcess.StartInfo.CreateNoWindow = true;
commandProcess.StartInfo.RedirectStandardError = true;
commandProcess.StartInfo.RedirectStandardInput = true;
commandProcess.StartInfo.RedirectStandardOutput = true;

if (commandProcess.Start()) { }

commandProcess.StandardInput.WriteLine(string.Format("shutdown /?"));
command