2011-10-04 106 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

相關問題