通过程序来操作iis生成网站或虚拟目录的操作集合。并设置网站具有net2.0属性。设置主目录等属性

通过程序来操作iis生成网站或虚拟目录的操作集合。并设置网站具有net2.0属性。设置主目录等属性
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.Collections;

namespace SetupClassLibrary
{
    public class WebHelper
    {
        static DirectoryEntry iisDE = new DirectoryEntry("IIS://localhost/W3SVC");
        ///<summary>
        /// Get The Location IIS WebServers Information
        ///</summary>
        ///<returns></returns>
        public static Hashtable GetLocationIIsWebServers()
        {
            Hashtable result = new Hashtable();
            GetWebSiteInfo(ref result);
            return result;
        }

        ///<summary>
        /// Create a new IIS Webserver
        ///</summary>
        ///<param name="webServerName">webserver name</param>
        ///<param name="path">webserver directory path</param>
        ///<param name="port">access port</param>
        public static void CreateNewIIsWebServer(string webServerName, string path, int port)
        {
            int siteID = GetWebSiteInfo(port);
            using (DirectoryEntry site = (DirectoryEntry)iisDE.Invoke("Create", "IIsWebServer", siteID))
            {
                site.Invoke("Put", "ServerComment", webServerName);
                site.Invoke("Put", "KeyType", "IIsWebServer");
                site.Invoke("Put", "ServerBindings", ":" + port.ToString() + ":");//第一个分号前面可以加ip地址。第二个分号后面可以添加主机头例如www.oa.com之类的网站名称
                site.Invoke("Put", "ServerState", 2);
                site.Invoke("Put", "FrontPageWeb", 1);
                site.Invoke("Put", "DefaultDoc", "index.aspx");
                site.Invoke("Put", "SecureBindings", ":443:");
                site.Invoke("Put", "ServerAutoStart", 1);
                site.Invoke("Put", "ServerSize", 1);
                site.Invoke("SetInfo");
                using (DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir"))
                {
                   siteVDir.Properties["AppIsolated"][0] = 2;
                siteVDir.Properties["Path"][0] = path;
                siteVDir.Properties["AccessFlags"][0] = 513;
                siteVDir.Properties["FrontPageWeb"][0] = 1;
                siteVDir.Properties["AppFriendlyName"][0] = webServerName;

                siteVDir.Properties["EnableDirBrowsing"][0] = false;
                siteVDir.Properties["AccessRead"][0] = true;
                siteVDir.Properties["AccessExecute"][0] = true;
                siteVDir.Properties["AccessWrite"][0] = false;
                siteVDir.Properties["AccessScript"][0] = true;//设置脚本访问
                siteVDir.Properties["DefaultDoc"][0] = "MyLogin.aspx";设置主目录

                siteVDir.Invoke("AppCreate", true);
                siteVDir.CommitChanges();

                //启动aspnet_iis.exe程序 使网站是net2.0的网站
                string fileName = Environment.GetEnvironmentVariable("windir") + @"/Microsoft.NET/Framework/v2.0.50727/aspnet_regiis.exe";
                ProcessStartInfo startInfo = new ProcessStartInfo(fileName);

                //处理目录路径
                string path2 = siteVDir.Path.ToUpper();
                int index = path2.IndexOf("W3SVC");
                path2 = path2.Remove(0, index);
                //启动aspnet_iis.exe程序,刷新教本映射
                startInfo.Arguments = "-s " + path2;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                Process process = new Process();
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                string errors = process.StandardError.ReadToEnd();
                if (errors != string.Empty)
                    throw new Exception(errors);
                Console.WriteLine(process.StandardOutput.ReadToEnd());                }
                site.CommitChanges();
            }

        }

 


        ///<summary>
        /// Create a new virtual directory
        ///</summary>
        ///<param name="website">webserver name</param>
        ///<param name="dirName">virtual directory name</param>
        ///<param name="properties">virtual dirctory properties</param>
        public static void CreateNewVirtualDir(string website, string dirName, System.Data.PropertyCollection properties)
        {
            if (GetVirtualDir(website, dirName)) throw new Exception(" The Virtual Dir is alread existed");
            using (DirectoryEntry de = GetWebSiteInfo(website))
            {
                using (DirectoryEntry vde = de.Children.Add(dirName, "IIsWebVirtualDir"))
                {
                    vde.CommitChanges();
                    de.CommitChanges();
                    UpdateVDirInfo(vde, ref properties);
                    vde.Invoke("AppCreate", true);
                    vde.CommitChanges();
                    de.CommitChanges();

                }
            }

        }

 

        ///<summary>
        /// Get Common virtual directory setting
        ///</summary>
        ///<param name="path">virtual directory path</param>
        ///<param name="vdir">virtual directory name</param>
        ///<returns></returns>
        public static System.Data.PropertyCollection GetDefaultVirtualDirSetting(string path, string vdir)
        {
            System.Data.PropertyCollection vProperty = new System.Data.PropertyCollection();
            //vProperty.Add("AnonymousUserName","");
            //vProperty.Add("AnonymousUserPass","");
            vProperty.Add("AccessRead", true);
            vProperty.Add("AccessExecute", false);
            vProperty.Add("AuthBasic", true);
            vProperty.Add("AuthNTLM", true);
            vProperty.Add("ContentIndexed", true);
            vProperty.Add("EnableDefaultDoc", true);
            vProperty.Add("EnableDirBrowsing", false);
            vProperty.Add("AccessSSL", false);
            vProperty.Add("AccessScript", false);
            vProperty.Add("DefaultDoc", "index.aspx");
            vProperty.Add("Path", path);
            vProperty.Add("AppIsolated", 2);
            vProperty.Add("AppFriendlyName", vdir);
            vProperty.Add("AccessFlags", 513);
            vProperty.Add("FrontPageWeb", 1);
            //vProperty.Add("DontLog", true);
            //vProperty.Add("AppRoot", "LM/W3SVC/" + siteID + "/" + vdir);
            return vProperty;
        }


        ///<summary>
        /// Delete the virtual directory in the WebServer
        ///</summary>
        ///<param name="website">webserver name</param>
        ///<param name="vdir">virtual directory name</param>
        public static void DeleteVirtualDir(string website, string vdir)
        {
            if (!GetVirtualDir(website, vdir)) throw new Exception(" The virtual directory don't exist in the website");
            using (DirectoryEntry de = GetWebSiteInfo(website))
            {
                foreach (DirectoryEntry sub in de.Children)
                {
                    if (sub.Name == vdir)
                    {
                        de.Invoke("Delete", new string[] { sub.SchemaClassName, vdir });
                        de.CommitChanges();
                    }
                }
            }
        }


        private static void UpdateVDirInfo(DirectoryEntry newDE, ref System.Data.PropertyCollection properties)
        {
            //newDE.Properties["AnonyMousUserName"][0] = properties["AnonymousUserName"].ToString();
            //newDE.Properties["AnonymousUserPass"][0] = properties["AnonymousUserPass"].ToString();
            newDE.Properties["AccessRead"][0] = (bool)properties["AccessRead"];
            newDE.Properties["AccessExecute"][0] = (bool)properties["AccessExecute"];
            newDE.Properties["AuthBasic"][0] = (bool)properties["AuthBasic"];
            newDE.Properties["AuthNTLM"][0] = (bool)properties["AuthNTLM"];
            newDE.Properties["ContentIndexed"][0] = (bool)properties["ContentIndexed"];
            newDE.Properties["EnableDefaultDoc"][0] = (bool)properties["EnableDefaultDoc"];
            newDE.Properties["EnableDirBrowsing"][0] = (bool)properties["EnableDirBrowsing"];
            newDE.Properties["AccessSSL"][0] = (bool)properties["AccessSSL"];
            newDE.Properties["AccessScript"][0] = (bool)properties["AccessScript"];
            newDE.Properties["DefaultDoc"][0] = properties["DefaultDoc"].ToString();
            newDE.Properties["Path"][0] = properties["Path"].ToString();
            newDE.Properties["AppIsolated"][0] = (int)properties["AppIsolated"];
            newDE.Properties["AppFriendlyName"][0] = properties["AppFriendlyName"].ToString();
            newDE.Properties["AccessFlags"][0] = (int)properties["AccessFlags"];
            newDE.Properties["FrontPageWeb"][0] = (int)properties["FrontPageWeb"];
            //newDE.Properties["DontLog"][0] = (bool)properties["DontLog"];
            //newDE.Properties["AppRoot"][0] = properties["AppRoot"].ToString();
        }

 

        private static bool GetVirtualDir(string webSite, string dirName)
        {
            bool result = false;
            using (DirectoryEntry de = GetWebSiteInfo(webSite))
            {

                if (de != null)
                {
                    foreach (DirectoryEntry subVD in de.Children)
                    {
                        if (subVD.SchemaClassName == "IIsWebVirtualDir" && subVD.Name == dirName)
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            return result;

        }

        private static void GetWebSiteInfo(ref Hashtable webServer)
        {
            DirectoryEntries des = iisDE.Children;
            foreach (DirectoryEntry subDE in des)
            {
                if (subDE.SchemaClassName == "IIsWebServer")
                {
                    webServer.Add(subDE.Properties["ServerComment"].Value.ToString(), subDE.Name);
                }
            }
            des = null;

        }

        private static DirectoryEntry GetWebSiteInfo(string website)
        {
            DirectoryEntry result = null;
            DirectoryEntries des = iisDE.Children;
            foreach (DirectoryEntry subDE in des)
            {
                if (subDE.SchemaClassName == "IIsWebServer" && subDE.Properties["ServerComment"].Value.ToString() == website)
                {
                    result = subDE.Children.Find("Root", "IIsWebVirtualDir");
                    break;
                }

            }
            des = null;
            return result;

        }

        private static int GetWebSiteInfo(int port)
        {
            int result = 1, i = 1;
            DirectoryEntries des = iisDE.Children;
            foreach (DirectoryEntry subDE in des)
            {
                if (subDE.SchemaClassName == "IIsWebServer")
                {
                    if ((i = Convert.ToInt32(subDE.Name)) >= result)
                    {
                        result = i + 1;
                    }
                    if (subDE.Properties["ServerBindings"][0].ToString() == ":" + port.ToString() + ":")
                    {
                        throw new Exception(" The port is already used");
                    }
                }
            }
            des = null;
            return result;
        }


    }
 
}

 --------------------------------------------------------------------------------------------------------------------------------------
public   static   void   CreateNewIIsWebServer(string   webServerName,   string   path,   int   port)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值