Manage DNS server by C# code

后端 未结 3 1773
无人及你
无人及你 2020-12-09 17:41

I need some sample code to create/delete zone and A record in microsoft DNS server by C#

3条回答
  •  死守一世寂寞
    2020-12-09 18:18

    You have to use WMI to invoke the DNSProvider.

    This to add a record:

     public void AddARecord(string hostName, string zone, string iPAddress, string dnsServerName)
     {
          ManagementScope scope = 
             new ManagementScope(@"\\" + dnsServerName + "\\root\\MicrosoftDNS");
    
          scope.Connect();
    
          ManagementClass cmiClass =
             new ManagementClass(scope, 
                                 new ManagementPath("MicrosoftDNS_AType"),
                                 null);
    
         ManagementBaseObject inParams = 
             cmiClass.GetMethodParameters("CreateInstanceFromPropertyData");
    
         inParams["DnsServerName"] = this.ServerName;
         inParams["ContainerName"] = zone;
         inParams["OwnerName"] = hostName + "." + zone;
         inParams["IPAddress"] = iPAddress;
    
         cmiClass.InvokeMethod("CreateInstanceFromPropertyData", inParams, null);
    }
    

    You can reference the WMI reference and extend this as you need using the methods and classes http://msdn.microsoft.com/en-us/library/ms682123(v=vs.85).aspx

提交回复
热议问题