资 源 简 介
Visual C#修改计算机名,修改电脑名称为另一个自定义的新名称,代码较简单,这里不多做介绍了,运行界面如示例截图所示。以下为核心部分代码,先睹为快,完整源码项目请下载:
[DllImport("kernel32.dll")]
private static extern int SetComputerName(string ipComputerName);//重写API函数
private void Frm_Main_Load(object sender, EventArgs e)
{
Computer computer = new Computer();//创建计算机对象
textBox1.Text = computer.Name;//显示计算机名称
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")//判断计算机名称是否为空
{
MessageBox.Show("计算机名称不能为空!");
}
else
{
SetComputerName(textBox2.Text);//修改计算机名称
MessageBox.Show("计算机名称修改成功,请重新启动计算机使之生效!");
}
}