下面是关于C#实现远程关闭和重启计算机的攻略和示例代码:
1.理论知识
要实现远程关闭和重启计算机,需要使用Windows API函数,主要包括以下两个函数:
- ExitWindowsEx:用于向本地或远程计算机发送关闭、注销、重启等命令;
- WNetAddConnection2:用于在本地计算机建立远程网络连接,将本地的指定目录与远程目录进行映射。
除此之外,还需要设置Windows防火墙例外规则,允许远程计算机在网络上访问本地计算机。
2.代码实现
2.1 远程关机
要远程关机计算机,需要获取远程计算机的网络地址和管理员账户的用户名和密码。下面是示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace RemoteControl
{
class Program
{
static void Main(string[] args)
{
string remoteComputer = "remote-pc"; // 远程计算机的名称或IP地址
string userName = "administrator"; // 管理员账户名
string password = "password"; // 管理员账户密码
// 在本地计算机上建立远程网络连接
using (new NetworkConnection(remoteComputer, userName, password))
{
// 向远程计算机发送关闭命令
ExitWindowsEx(0x00000008, 0);
}
}
// Windows API函数
[DllImport("user32.dll", SetLastError = true)]
static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
}
// 建立远程网络连接的类
public class NetworkConnection : IDisposable
{
string _networkName;
public NetworkConnection(string networkName, string userName, string password)
{
_networkName = networkName;
// 建立远程网络连接
var netResource = new NetResource
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplayType.Share,
RemoteName = _networkName
};
var userNamePtr = Marshal.StringToBSTR(userName);
var passwordPtr = Marshal.StringToBSTR(password);
var credential = new Credential
{
UserName = userName,
Password = password,
Type = CredentialType.DomainPassword
};
int result = WNetAddConnection2(netResource, passwordPtr, userNamePtr, 0);
if (result != 0)
{
throw new System.ComponentModel.Win32Exception(result);
}
}
// 释放远程网络连接
public void Dispose()
{
WNetCancelConnection2(_networkName, 0, true);
}
// Windows API函数
[DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int WNetAddConnection2(NetResource netResource, string password, string username, uint flags);
[DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int WNetCancelConnection2(string name, int flags, bool force);
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplayType DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplayType : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
[Flags]
public enum CredentialType : uint
{
None = 0x0,
DomainPassword = 0x1,
DomainCertificate = 0x2,
DomainVisiblePassword = 0x4,
Generic = 0x5,
DomainExtended = 0x6,
Maximum = 0x7fffffff,
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct Credential
{
public CredentialType Type;
public string TargetName;
public string Comment;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
public byte[] CredentialBlob;
public uint Persist;
public string UserName;
public string Password;
}
}
}
2.2 远程重启
要远程重启计算机,需要在上面示例代码的基础上增加一个超时时长参数,以便在发送重启命令后等待远程计算机重新启动。下面是示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace RemoteControl
{
class Program
{
static void Main(string[] args)
{
string remoteComputer = "remote-pc"; // 远程计算机的名称或IP地址
string userName = "administrator"; // 管理员账户名
string password = "password"; // 管理员账户密码
// 在本地计算机上建立远程网络连接
using (new NetworkConnection(remoteComputer, userName, password))
{
// 向远程计算机发送重启命令
ExitWindowsEx(0x00000002, 0);
// 等待远程计算机重新启动
Task.Delay(180 * 1000).Wait();
}
}
// Windows API函数
[DllImport("user32.dll", SetLastError = true)]
static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
}
// 建立远程网络连接的类,代码同上
}
3.总结
上述代码可以实现通过C#语言远程关闭和重启计算机,需要注意的是,在远程计算机上需要运行管理员权限的服务或服务进程,才能够正常接收到来自远程计算机的关闭和重启命令。同时还需要注意防火墙设置,允许对应端口的网络访问即可。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现远程关闭和重启计算机的示例代码 - Python技术站