How Do I Delete a DNS Domain Programmatically?

I am building a C# web app to manage our DNS servers and am using the WMI Namespace for everything. The only thing I am having trouble with is deleting DNS Domains. Here is my code:

internal static bool DeleteDomainFromDns(string DnsServerName, string ContainerName, string Name)
    {
        try
        {
            string Query = "SELECT * FROM MicrosoftDNS_Domain WHERE DnsServerName = '" + DnsServerName + "' AND ContainerName = '" + ContainerName + "' AND Name = '" + Name + "'";
            ObjectQuery qry = new ObjectQuery(Query);
            DnsProvider dns = new DnsProvider();
            ManagementObjectSearcher s = new ManagementObjectSearcher(dns.Session, qry);
            ManagementObjectCollection col = s.Get();
            dns.Dispose();

            foreach (ManagementObject obj in col)
            {
                obj.Delete(); //Exception occurs here
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

The error I get is: ManagementException was caught "Generic Failure". I've read online where people are deleting domains by using the zone namespace but that only works if the domain you want to delete is a zone itself. I need to delete domains that are not zones. Can anyone help?


我还没有找到一种方法来使用WMI删除一个域,并且还检入了名为DNSShell的PowerShell管理单元,但它看起来不像是有一条删除域的命令。


You can try with the script DnsResource.vbs from Delete a Resource Record. It uses only DNS WMI Provider. So if it will work for your porpoise you can do the same in your C# program.

You can also consider to use DnsModifyRecordsInSet. In Windos SDK (C:Program FilesMicrosoft SDKsWindowsv7.1Samplesnetdsdnsmodifyrecords) you can find an example in C++ which use DnsModifyRecordsInSet . It demonstrate how to add a record in the DNS. If you use the second parameter pDeleteRecords instead of the first pAddRecords you will be able to delete any record in the DNS.

链接地址: http://www.djcxy.com/p/3192.html

上一篇: 在MIPS64中加载地址

下一篇: 如何以编程方式删除DNS域?