1. 使用命名空间:
using System.Runtime.InteropServices;
2. 在类中声明:
[DllImport("kernel32.dll", EntryPoint = "SetComputerNameEx")]
public static extern int apiSetComputerNameEx(int type, string lpComputerName);
3. 在实现代码处调用:
apiSetComputerNameEx(5, "TestComputerNameModification");
样例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("kernel32.dll", EntryPoint = "SetComputerNameEx")]
public static extern int apiSetComputerNameEx(int type, string lpComputerName);
static void Main(string[] args)
{
int rtn = apiSetComputerNameEx(5, "TestComputerNameModification");
System.Console.WriteLine("Computer name set result="+rtn);
}
}
}
4. 修改计算机名,需要重启操作系统才能生效。