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

发布于 2024-09-29 17:43:12 字数 312 浏览 0 评论 0原文

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

有什么帮助吗?

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

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

I'm working on an auto-configure app for new PC's. I managed to programatically set all the needed parameters, except Primary DNS Suffix of a computer (please remember that this is different from the network connection default DNS suffix).

Any help?

Update: Here's requested class for setting IP, Subnet, Gateway, and DNS of the PC.
http://pastebin.com/fHACwwDV

It was tested only in system having 1 network interface, I don't know how it'll behave when there are more NICs. Oh, and it's WIP, so for now no error handling apart from throwing exception was implemented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-10-06 17:43:13

是的,它成功了。当然,我一如既往地愚蠢,因为我已经设置了主机名。
以下是 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

Yay, it worked. Of course I'm stupid as always, because I was already setting the host name.
Here's full code for host/suffix change in 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文