2010-10-28 78 views
0

我正在爲新PC的自動配置應用程序。我設法以編程方式設置除計算機的主DNS後綴之外的所有必需參數(請記住,這與網絡連接默認DNS後綴不同)。如何在.NET中更改計算機的主DNS後綴?

任何幫助?

更新:這是要求的類別,用於設置PC的IP,子網,網關和DNS。 http://pastebin.com/fHACwwDV

它僅在具有1個網絡接口的系統中測試過,我不知道當有更多的NIC時它將如何表現。哦,這是在製品,所以現在除了拋出異常之外沒有執行任何錯誤處理。

+0

能否請您補充一點,節目有已完成所有其他配置文件鏈接?爲他人提供真正有用的信息。 – jgauffin 2010-10-28 12:42:16

回答

0

耶,它的工作。當然,我一如既往愚蠢,因爲我已經設置了主機名。 下面是在vb.net主機/後綴改變全碼:

Private Enum COMPUTER_NAME_FORMAT As Integer 
    ComputerNameNetBIOS = 0 
    ComputerNameDnsHostname = 1 
    ComputerNameDnsDomain = 2 
    ComputerNameDnsFullyQualified = 3 
    ComputerNamePhysicalNetBIOS = 4 
    ComputerNamePhysicalDnsHostname = 5 
    ComputerNamePhysicalDnsDomain = 6 
    ComputerNamePhysicalDnsFullyQualified = 7 
End Enum 

Public Const MAX_COMPUTERNAME_LENGTH As Int32 = 31 

<DllImport("kernel32.dll", CharSet:=CharSet.Ansi, SetLastError:=True)> _ 
Private Shared Function SetComputerNameEx(_ 
    ByVal NameType As COMPUTER_NAME_FORMAT, _ 
    <MarshalAs(UnmanagedType.LPStr)> ByVal lpBuffer As String) As Integer 
End Function 

Public Function SetNewName(ByVal Computername As String, ByVal DNSSuffix As String) As Boolean 
    If NetworkSet.SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, _ 
     Computername & Convert.ToChar(0)) = 0 Then 
     Throw New Win32Exception 
    End If 
    If NetworkSet.SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsDomain, _ 
     DNSSuffix & Convert.ToChar(0)) = 0 Then 
     Throw New Win32Exception 
    End If 

End Function