c# – 如何以编程方式添加DNS别名?

我想在Microsoft的DNS服务器中创建一个别名记录,将AliasA指向ComputerA.我该如何以编程方式执行此操作? 最佳答案 我使用WMI来做这件事,在网上找到了一个例子,这就是它的样子.

   private ManagementScope _session = null;

   public ManagementPath CreateCNameRecord(string DnsServerName, string ContainerName, string OwnerName, string PrimaryName)
    {
        _session = new ManagementScope("\\\\" + DnsServerName+ "\\root\\MicrosoftDNS", con);
        _session.Connect();

        ManagementClass zoneObj = new ManagementClass(_session, new ManagementPath("MicrosoftDNS_CNAMEType"), null);
        ManagementBaseObject inParams = zoneObj.GetMethodParameters("CreateInstanceFromPropertyData");
        inParams["DnsServerName"] = ((System.String)(DnsServerName));
        inParams["ContainerName"] = ((System.String)(ContainerName));
        inParams["OwnerName"] = ((System.String)(OwnerName));
        inParams["PrimaryName"] = ((System.String)(PrimaryName));
        ManagementBaseObject outParams = zoneObj.InvokeMethod("CreateInstanceFromPropertyData", inParams, null);

        if ((outParams.Properties["RR"] != null))
        {
            return new ManagementPath(outParams["RR"].ToString());
        }

        return null;
    }
点赞