2011-10-04 47 views
8

我需要通过.net应用程序重命名我的计算机。 我试过这个代码:以编程方式重命名计算机c#.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

“没有工作” 的意思....错误? – Shoban

+0

您是否必须重新启动计算机才能真正反映这些更改?或者你有一些错误? –

+0

@Olia如果可能的话,通过第三方应用更改计算机名称会导致很多问题。 – Mob

回答

6

我已经试过我已经找到了所有的方法来改变计算机名称和没有人的作品.....它不会改变计算机名称... 它的唯一工作方式是当我chaged一些注册表键值,这是代码,这样做可以吗?

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

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

3

SetComputerName的MSDN文档..

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

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

+0

是的......我确实重启了 –

1

Programmatically renaming a computer using C#

这是一个很长的文章,我不知道究竟会是直接相关的,所以我不会糊片段

+3

虽然这在理论上可以回答这个问题,但[最好](http://meta.stackexchange。com/q/8259)在这里包括答案的基本部分,并提供参考链接。 –

+0

本文中的函数会更改计算机的名称,但会引发此异常 - 未找到网络路径。 (从HRESULT异常:0x80070035) –

2

一个WMI对象设置计算机名称。然后使用注册表检查名称是否已设置。因为System.Environment.MachineName没有立即更新。 而CMD.exe中的'hostname'命令仍然输出旧名称。因此重新启动仍然是必需的。但随着注册表检查可以看到名称是否设置。

希望这会有所帮助。

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

它很难使用任何外部方法来更新电脑的名字,由于系统的保护。最好的办法是使用WMIC.exe自带的实用程序来重命名PC。只需从C#启动wmic.exe,并将rename命令作为参数传递即可。

>

public void SetMachineName(string newName) 
{ 

    // Create a new process 
    ProcessStartInfo process = new ProcessStartInfo(); 

    // set name of process to "WMIC.exe" 
    process.FileName = "WMIC.exe"; 

    // pass rename PC command as argument 
    process.Arguments = "computersystem where caption='" + System.Environment.MachineName + "' rename " + newName; 

    // Run the external process & wait for it to finish 
    using (Process proc = Process.Start(process)) 
    { 
     proc.WaitForExit(); 

     // print the status of command 
     Console.WriteLine("Exit code = " + proc.ExitCode); 
    } 
} 
+0

你的问题是什么?什么似乎是你遇到的具体问题? – TVOHM

相关问题