以编程方式重命名计算机c#.net
我需要通过 .net 应用程序重命名我的计算机。 我已经尝试过这段代码:
public static bool SetMachineName(string newName)
{
MessageBox.Show(String.Format("Setting Machine Name to '{0}'...", newName));
// Invoke WMI to populate the machine name
using (ManagementObject wmiObject = new ManagementObject(new ManagementPath(String.Format("Win32_ComputerSystem.Name='{0}'",System.Environment.MachineName))))
{
ManagementBaseObject inputArgs = wmiObject.GetMethodParameters("Rename");
inputArgs["Name"] = newName;
// Set the name
ManagementBaseObject outParams = wmiObject.InvokeMethod("Rename",inputArgs,null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if (ret == 0)
{
//worked
return true;
}
else
{
//didn't work
return false;
}
}
}
但它不起作用。
我已经尝试过这个:
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern bool SetComputerName(string lpComputerName);
public static bool SetMachineName(string newName)
{
bool done = SetComputerName(newName);
if (done)
{
{ MessageBox.Show("Done"); return true; }
}
else
{ MessageBox.Show("Failed"); return false; }
}
但它也不起作用。
I need to rename my computer via .net application.
I have tried this code:
public static bool SetMachineName(string newName)
{
MessageBox.Show(String.Format("Setting Machine Name to '{0}'...", newName));
// Invoke WMI to populate the machine name
using (ManagementObject wmiObject = new ManagementObject(new ManagementPath(String.Format("Win32_ComputerSystem.Name='{0}'",System.Environment.MachineName))))
{
ManagementBaseObject inputArgs = wmiObject.GetMethodParameters("Rename");
inputArgs["Name"] = newName;
// Set the name
ManagementBaseObject outParams = wmiObject.InvokeMethod("Rename",inputArgs,null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if (ret == 0)
{
//worked
return true;
}
else
{
//didn't work
return false;
}
}
}
but it didn't work.
and i have tried this one:
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern bool SetComputerName(string lpComputerName);
public static bool SetMachineName(string newName)
{
bool done = SetComputerName(newName);
if (done)
{
{ MessageBox.Show("Done"); return true; }
}
else
{ MessageBox.Show("Failed"); return false; }
}
but it also didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经尝试了所有我发现的更改计算机名称的方法,但没有一个有效......它不会更改计算机名称......
它起作用的唯一方法是当我更改一些注册表项值时,这是代码,可以这样做吗?
重新启动后名称发生变化......
I have tried all the ways i have found to change computer name and no one works.....it doesn't change the computer name...
the only way it worked is when i chaged some registry key values , this is the code , is it ok to do so?
and after the restart the name changes....
WMI 对象设置计算机名称。然后使用注册表来检查该名称是否已设置。因为 System.Environment.MachineName 不会立即更新。
CMD.exe 中的“主机名”命令仍然输出旧名称。所以还是需要重启。但通过注册表检查可以查看该名称是否已设置。
希望这有帮助。
A WMI objects sets the computer name. Then the registry is used to check whether the name was set. Because the System.Environment.MachineName is not updated right away.
And the 'hostname' command in CMD.exe still outputs the old name. So a reboot is still required. But with the registry check can see if the name was set.
Hope this helps.
来自 SetComputerName< 的 MSDN 文档/a>..
您尝试重新启动计算机吗?
From the MSDN Documentation of SetComputerName..
Did you try restarting the computer?
使用 C# 以编程方式重命名计算机
这是一篇很长的文章,我不确定到底什么是直接相关的,所以我不会粘贴片段
Programmatically renaming a computer using C#
It is a long article and I'm not sure what exactly will be directly relevant so I won't paste a snippet