How to change Primary DNS Suffix of a computer in .NET?
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 differe开发者_如何学运维nt 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.
From a search on msdn, looks like you'll need to use SetComputerNameEx.
pinvoke.net spells out what you'll need to do to access this from C#.
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
精彩评论