11

我需要通过 .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; }
}

但它也没有用。

4

4 回答 4

11

我已经尝试了所有我发现的更改计算机名称的方法,但没有人可以工作......它不会改变计算机名称......它唯一有效的方法是当我更改一些注册表项值时,这就是代码,这样做可以吗?

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;
}

重新启动后名称更改....

于 2011-10-05T12:31:10.733 回答
3

WMI 对象设置计算机名称。然后使用注册表检查名称是否被设置。因为 System.Environment.MachineName 没有立即更新。并且 CMD.exe 中的“主机名”命令仍然输出旧名称。所以仍然需要重新启动。但是通过注册表检查可以查看名称是否已设置。

希望这可以帮助。

Boolean SetComputerName(String Name)  
{  
String RegLocComputerName = @"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName";
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;  
}
于 2012-09-17T07:52:27.883 回答
2

来自SetComputerName的 MSDN 文档。

为本地计算机设置一个新的 NetBIOS 名称。名称存储在注册表中,名称更改将在用户下次重新启动计算机时生效。

您是否尝试重新启动计算机?

于 2011-10-04T10:48:01.077 回答
0

使用 C# 以编程方式重命名计算机

这是一篇很长的文章,我不确定什么是直接相关的,所以我不会粘贴一个片段

于 2011-10-04T10:48:53.457 回答