我需要通过。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; }
}
我已经尝试了我发现的所有方法来改变计算机名称,没有人工作.....它不会改变计算机名称……它工作的唯一方法是当我改变一些注册表项值,这是代码,这样做是可以的吗?
public static bool SetMachineName(string newName)
{
RegistryKey key = Registry.LocalMachine;
string activeComputerName = "SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName";
RegistryKey activeCmpName = key.CreateSubKey(activeComputerName);
activeCmpName.SetValue("ComputerName", newName);
activeCmpName.Close();
string computerName = "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName";
RegistryKey cmpName = key.CreateSubKey(computerName);
cmpName.SetValue("ComputerName", newName);
cmpName.Close();
string _hostName = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters\";
RegistryKey hostName = key.CreateSubKey(_hostName);
hostName.SetValue("Hostname",newName);
hostName.SetValue("NV Hostname",newName);
hostName.Close();
return true;
}
和重启后的名称更改....
WMI对象设置计算机名称。然后使用注册表检查是否设置了名称。因为System.Environment.MachineName没有立即更新。CMD.exe中的"hostname"命令仍然输出旧的名称。所以仍然需要重新启动。但是通过注册表检查可以看到是否设置了名称。
希望对你有帮助。
Boolean SetComputerName(String Name)
{
String RegLocComputerName = @"SYSTEMCurrentControlSetControlComputerNameComputerName";
try
{
string compPath= "Win32_ComputerSystem.Name='" + System.Environment.MachineName + "'";
using (ManagementObject mo = new ManagementObject(new ManagementPath(compPath)))
{
ManagementBaseObject inputArgs = mo.GetMethodParameters("Rename");
inputArgs["Name"] = Name;
ManagementBaseObject output = mo.InvokeMethod("Rename", inputArgs, null);
uint retValue = (uint)Convert.ChangeType(output.Properties["ReturnValue"].Value, typeof(uint));
if (retValue != 0)
{
throw new Exception("Computer could not be changed due to unknown reason.");
}
}
RegistryKey ComputerName = Registry.LocalMachine.OpenSubKey(RegLocComputerName);
if (ComputerName == null)
{
throw new Exception("Registry location '" + RegLocComputerName + "' is not readable.");
}
if (((String)ComputerName.GetValue("ComputerName")) != Name)
{
throw new Exception("The computer name was set by WMI but was not updated in the registry location: '" + RegLocComputerName + "'");
}
ComputerName.Close();
ComputerName.Dispose();
}
catch (Exception ex)
{
return false;
}
return true;
}
来自SetComputerName..的MSDN文档
为本地计算机设置新的NetBIOS名称。名称存储在注册表和名称更改将在用户下次登录时生效重新启动计算机。
您是否尝试重新启动计算机?
使用c#编程重命名计算机
这是一篇很长的文章,我不确定什么是直接相关的,所以我不会粘贴一个片段