C#操作服务器(6):C#创建、启动和停止IIS的Web站点

作者:陆金龙    发表时间:2015-05-24 12:38   


如果出现0x80005000错误,需在“打开或关闭Windows功能”中,为“Internet信息服务”启用“IIS 元数据库和IIS 6配置兼容性”。

public class IIsMgr
{
    /// 
    /// 创建Web网站
    /// 
    ///.Net 运行时版本: v4.0 或 v2.0
    /// 站点Id
    public static string AddWebSite(string siteName, int sitePort, string crlVersion, string physicalPath)
    {
        ServerManager srvmgr = new ServerManager();
        Site site = srvmgr.Sites.Add(siteName, "http", string.Format("*:{0}:", sitePort), physicalPath);
        ApplicationPool pool = srvmgr.ApplicationPools.Add(siteName);
        pool.ManagedRuntimeVersion = crlVersion;
        foreach (Microsoft.Web.Administration.Application app in site.Applications)
        {
            app.ApplicationPoolName = pool.Name;
        }
        srvmgr.CommitChanges();
        srvmgr.Dispose();
        return site.Id.ToString();
    }
    public static bool RemoveWebSite(string siteName)
    {
        string siteID = GetWebSiteID(siteName);
        DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC");
        try
        {
            DirectoryEntry deEntry = new DirectoryEntry();
            deRoot.RefreshCache();
            deEntry = deRoot.Children.Find(siteID, "IIsWebServer");
            deRoot.Children.Remove(deEntry);
            deRoot.CommitChanges();
            deRoot.Close();
            return true;
        }
        catch (System.Exception)
        {
            return false;
        }
    }
        
    public static void StartWebSite(string siteName)
    {
        string siteID = GetWebSiteID(siteName);
        DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/W3SVC/" + siteID);
        siteEntry.Invoke("Start", new object[] { });
    }
        
    public static void StopWebSite(string siteName)
    {
        string siteID = GetWebSiteID(siteName);
        DirectoryEntry siteEntry = new DirectoryEntry("IIS://localhost/W3SVC/" + siteID);
        siteEntry.Invoke("Stop", new object[] { });
    }
        
    public static string GetWebSiteID(string siteName)
    {
        Regex regex = new Regex(siteName);
        string tmpStr;
        DirectoryEntry deEntry = new DirectoryEntry("IIS://localhost/W3SVC/".TrimEnd('/'));

        foreach (DirectoryEntry child in deEntry.Children)
        {
            if (child.SchemaClassName == "IIsWebServer")
            {
                if (child.Properties["ServerBindings"].Value != null)
                {
                    tmpStr = child.Properties["ServerBindings"].Value.ToString();

                    if (regex.Match(tmpStr).Success)
                    {
                        return child.Name;
                    }
                }

                if (child.Properties["ServerComment"].Value != null)
                {
                    tmpStr = child.Properties["ServerComment"].Value.ToString();

                    if (regex.Match(tmpStr).Success)
                    {
                        return child.Name;
                    }
                }
            }
        }
        return null;
    }   
}