以编程方式重命名计算机c#.net

发布于 2024-12-08 04:16:09 字数 1340 浏览 2 评论 0原文

我需要通过 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉宸 2024-12-15 04:16:09

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

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

重新启动后名称发生变化......

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?

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

and after the restart the name changes....

黯然 2024-12-15 04:16:09

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

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.

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;  
}
迎风吟唱 2024-12-15 04:16:09

来自 SetComputerName< 的 MSDN 文档/a>..

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

您尝试重新启动计算机吗?

From the MSDN Documentation of SetComputerName..

Sets a new NetBIOS name for the local computer. The name is stored in
the registry and the name change takes effect the next time the user
restarts the computer.

Did you try restarting the computer?

早乙女 2024-12-15 04:16:09

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文