【问题标题】:rename computer programmatically c# .net以编程方式重命名计算机 c# .net
【发布时间】:2011-11-30 14:13:40
【问题描述】:

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

但它也没有工作。

【问题讨论】:

  • “没有工作”的意思是....错误?
  • 是否必须重新启动计算机才能真正反映更改?还是您遇到了一些错误?
  • @Olia 如果可能的话,通过第三方应用程序更改计算机名称会导致很多问题。
  • 代码以第二种方式正常工作,没有例外,但重新启动后名称不会改变....在第一种方式中,ret值是!= 0,我得到错误 - -> 没用...
  • 当我以第二种方式重命名计算机名称时,它不会改变 MyComuter 的属性,但是当我在 .net 中获取计算机名称时,我会看到新名称(更改名称.. .),怎么可能?

标签: c# .net wmi kernel32


【解决方案1】:

来自 SetComputerName.. 的 MSDN 文档

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

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

【讨论】:

    【解决方案2】:

    Programmatically renaming a computer using C#

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

    【讨论】:

    • 虽然理论上可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
    • 本文中更改计算机名称的函数抛出此异常-找不到网络路径。 (HRESULT 异常:0x80070035)
    【解决方案3】:

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

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

    重启后名字就变了....

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-11
        • 2017-11-26
        • 1970-01-01
        • 2011-11-02
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2013-09-29
        相关资源
        最近更新 更多