通过代码方式操作IIS7

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Web.Administration;

namespace TestIISTools
{
    /// 
    /// iis7 support
    /// 
    public class IISTool
    {   /// 
        /// 新建网站
        /// 
        public static void CreateIISWeb()
        {


            ServerManager serverMgr = new ServerManager();
            Site mySite = serverMgr.Sites.Add("Test", "d:\\test", 8181);
            //serverMgr.ApplicationPools.Add("MyAppPool");
            //mySite.ApplicationDefaults.ApplicationPoolName = "MyAppPool";
            mySite.TraceFailedRequestsLogging.Enabled = true;
            mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
            serverMgr.CommitChanges();


        }
        /// 
        /// 新建应用程序池
        /// 
        public static void CreateAppPool()
        {
            ServerManager serverMgr = new ServerManager();
            serverMgr.ApplicationPools.Add("MyAppPool");
            serverMgr.CommitChanges();
            
        }
        /// 
        /// 删除应用程序池
        /// 
        public static void DeleteAppPool()
        {
            ServerManager serverMgr = new ServerManager();
            ApplicationPool appPool = serverMgr.ApplicationPools["MyAppPool"];
            serverMgr.ApplicationPools.Remove(appPool);
            serverMgr.CommitChanges();
        }
        /// 
        /// 删除网站
        /// 
        public static void DeleteIISWeb()
        {
            ServerManager serverMgr = new ServerManager();
            Site s1 = serverMgr.Sites["Test"]; // you can pass the site name or the site ID
            serverMgr.Sites.Remove(s1);
            //ApplicationPool appPool= serverMgr.ApplicationPools["MyAppPool"];
            //serverMgr.ApplicationPools.Remove(appPool);
            serverMgr.CommitChanges();
        }

        private static VirtualDirectory virtualDirectory = null;
        /// 
        ///  添加虚拟目录
        /// 
        public static void CreateVirtualDirectory()
        {
            if (virtualDirectory == null)
            {
                ServerManager iisManager = new ServerManager();
                Application app = iisManager.Sites[0].Applications[0];
                //virtualDirectory use to delete
                virtualDirectory = app.VirtualDirectories.Add("/test", "d:\\test");
                iisManager.CommitChanges();
            }
        }
        /// 
        /// 删除虚拟目录
        /// 
        public static void DeleteVirtualDirectory()
        {
            if (virtualDirectory != null)
            {
                ServerManager iisManager = new ServerManager();
                Application app = iisManager.Sites[0].Applications[0];
                app.VirtualDirectories.Remove(virtualDirectory);
                virtualDirectory = null;
                iisManager.CommitChanges();
            }
  
        }
     
    }
}

你可能感兴趣的:(c#用法技巧)