如何在.NET中更改计算机的主DNS后缀?

时间:2010-10-28 12:34:46

标签: c# .net vb.net wmi

我正在开发一款适用于新PC的自动配置应用。我设法以编程方式设置所有必需的参数,除了计算机的主DNS后缀(请记住,这与网络连接默认DNS后缀不同)。

任何帮助?

更新:这是设置PC的IP,子网,网关和DNS的请求类。 http://pastebin.com/fHACwwDV

它仅在具有1个网络接口的系统中进行了测试,我不知道当有更多NIC时它会如何表现。哦,它是WIP,所以现在除了抛出异常之外没有实现错误处理。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分: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