2010-02-24 57 views
1

我想在Windows 2000上使用.NET 2.0更改計算機的名稱(主機名)。計算機未加入域。如何更改.NET 2.0中的Windows 2000計算機名稱?

Windows XP及更高版本提供了WMI方法Win32_ComputerSystem.Rename,但這在Windows 2000(reference here)中不可用。

如果需要的話,我並不反對僅僅調用一個外部程序,但我似乎也找不到能在Windows 2000上工作的程序。在Google上搜索似乎沒有任何明顯的變化。

在此先感謝。

回答

3

我認爲Windows的API可能是幫助在Windows 2000:使用SetComputerNameEx

BOOL WINAPI SetComputerNameEx(
    __in COMPUTER_NAME_FORMAT NameType, 
    __in LPCTSTR lpBuffer 
); 

該樣本是根據樣本上pinvoke.net

public class RenameComputer 
{ 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
    static extern bool SetComputerNameEx(COMPUTER_NAME_FORMAT NameType, string lpBuffer); 

    enum COMPUTER_NAME_FORMAT 
    { 
     ComputerNameNetBIOS, 
     ComputerNameDnsHostname, 
     ComputerNameDnsDomain, 
     ComputerNameDnsFullyQualified, 
     ComputerNamePhysicalNetBIOS, 
     ComputerNamePhysicalDnsHostname, 
     ComputerNamePhysicalDnsDomain, 
     ComputerNamePhysicalDnsFullyQualified, 
    } 

    //ComputerNamePhysicalDnsHostname used to rename the computer name and netbios name before domain join 
    public static bool Rename(string name) 
    { 
     bool result = SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, name); 
     if (!result) 
      throw new Win32Exception(); 

     return result; 
    } 
} 

另外要調用WinAPI,您也可以使用Process.Startnetsh命令的組合,如here所述。

+0

阿哈,我敢肯定這會奏效,我會盡快接受這一點。 – 2010-02-25 12:18:00

0

在那個鏈接下面有一個例子。你可以的PInvoke是

相關問題