从dns区域删除主机

时间:2015-09-21 12:45:20

标签: c# dns wmi

我正在尝试使用以下代码从所选区域删除主机,但不删除。

     string Query = "SELECT * FROM MicrosoftDNS_Zone WHERE ContainerName = '" + ZoneName    + "' and OwnerName='" + recordName + "'";

      ObjectQuery qry = new ObjectQuery(Query);
      ManagementScope scope = new ManagementScope(@"\\" + dnsServerName + "\\root\\MicrosoftDNS");
      scope.Connect();
      ManagementObjectSearcher s = new ManagementObjectSearcher(scope, qry);
            ManagementObjectCollection col = s.Get();
            foreach (ManagementObject obj in col)
            {
                obj.Delete();
            }

请帮忙。

1 个答案:

答案 0 :(得分:0)

下面是删除域记录的代码。

 bool DeleteRecordFromDns(string ZoneName, string dnsServerName, string recordName)
    {
        try
        {
            string Query = string.Format("SELECT * FROM MicrosoftDNS_AType WHERE OwnerName = '{0}.{1}'", recordName, ZoneName);
            ObjectQuery qry = new ObjectQuery(Query);
            ManagementScope scope = new ManagementScope(@"\\" + dnsServerName + "\\root\\MicrosoftDNS");
            scope.Connect();
            ManagementObjectSearcher s = new ManagementObjectSearcher(scope, qry);
            ManagementObjectCollection col = s.Get();
            if (col.Count > 0)
            {
                foreach (ManagementObject obj in col)
                {
                    obj.Delete();
                }
                return true;
            }
            else
            {
                return false;
            }

        }
        catch (Exception)
        {
            return false;
        }
    }