一个用Com管理DNS的简单类

public class DNS : System.IDisposable
{
    string server = ".";
    string userName = null;
    string Password = null;
    WbemScripting.SWbemLocatorClass sc = null;
    WbemScripting.SWbemServices svs = null;
    WbemScripting.SWbemObject dnsSrv = null;
    bool isConneced = false;

    public string Server
    {
        get
        {
            return server;
        }
        set
        {
            server = value;
        }
    }

    public DNS()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public DNS(string server)
    {
        this.server = server;
    }

    public DNS(string server, string username, string pwd)
    {
        this.server = server;
        this.userName = username;
        this.Password = pwd;
    }

    ~DNS()
    {
        sc = null;
        svs = null;
        dnsSrv = null;
    }

    public bool Connect()
    {
        sc = new WbemScripting.SWbemLocatorClass();
        try
        {
            svs = sc.ConnectServer(server, @"\root\MicrosoftDNS", userName, Password, "", "", 0, null);
            dnsSrv = svs.Get("MicrosoftDNS_Server.Name='" + server + "'", 0, null);
            this.isConneced = true;
        }
        catch (Exception e)
        {
            //System.Web.HttpContext.Current.Response.Write(e);
            Console.WriteLine(e);
            this.isConneced = false;
        }
        return this.isConneced;
    }

    public Exception AddARecord(string Domain, string HostName, string IPAddress)
    {
        if (!this.isConneced)
            return new Exception("Not Connected!");

        try
        {
            object strRR = HostName + "." + Domain + ". IN A " + IPAddress;
            object srv = server as Object;
            object dm = Domain as Object;
            //System.Web.HttpContext.Current.Response.Write(strRR+"<br>");
            WbemScripting.SWbemObject objRR = svs.Get("MicrosoftDNS_ResourceRecord", 0, null);

            WbemScripting.SWbemMethod method = objRR.Methods_.Item("CreateInstanceFromTextRepresentation", 0);
            WbemScripting.SWbemObject inparam = method.InParameters;
            inparam.Properties_.Add("DnsServerName", WbemScripting.WbemCimtypeEnum.wbemCimtypeString, false, 0);
            inparam.Properties_.Add("ContainerName", WbemScripting.WbemCimtypeEnum.wbemCimtypeString, false, 0);
            inparam.Properties_.Add("TextRepresentation", WbemScripting.WbemCimtypeEnum.wbemCimtypeString, false, 0);
            inparam.Properties_.Item("DnsServerName", 0).set_Value(ref srv);
            inparam.Properties_.Item("ContainerName", 0).set_Value(ref dm);
            inparam.Properties_.Item("TextRepresentation", 0).set_Value(ref strRR);

            objRR.ExecMethod_("CreateInstanceFromTextRepresentation", inparam, 0, null);
            return null;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            return ex;
        }
        finally
        {
        }
    }

    public void StartDNSService()
    {
        this.dnsSrv.ExecMethod_("StartService", null, 0, null);
    }

    public void StopDNSService()
    {
        this.dnsSrv.ExecMethod_("StopService", null, 0, null);
    }

    public void StartScavenging()
    {
        this.dnsSrv.ExecMethod_("StartScavenging", null, 0, null);
    }

    #region IDisposable 成员
    public void Dispose()
    {
        sc = null;
        svs = null;
        dnsSrv = null;
    }
    #endregion
}
Contributors: FHL