您好,欢迎来到暴趣科技网。
搜索
您的当前位置:首页C#如何来创建IIS虚拟目录

C#如何来创建IIS虚拟目录

来源:暴趣科技网

ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost  

Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC"  

WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,则从1开始依次类推。 

 using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
 namespace Install_IIS
{
    class IISManager
    {
        public IISManager()
        {
        }
        /// <summary>
        /// 创建虚拟目录
        /// </summary>
        /// <param name="WebSite">服务器站点名称</param>
        /// <param name="VDirName">虚拟目录名称</param>
        /// <param name="Path"></param>
        /// <param name="RootDir"></param>
        /// <param name="chkRead"></param>
        /// <param name="chkWrite"></param>
        /// <param name="chkExecute"></param>
        /// <param name="chkScript"></param>
        /// <param name="chkAuth"></param>
        /// <param name="webSiteNum">1</param>
        /// <param name="serverName">localhost</param>
        /// <returns></returns>
        public string CreateVDir(string WebSite,string VDirName, string Path, bool RootDir, bool chkRead,bool chkWrite, bool chkExecute, bool chkScript, bool chkAuth, int webSiteNum, string serverName)
        {
            string sRet=String.Empty;
           
            System.DirectoryServices.DirectoryEntry IISSchema;
            System.DirectoryServices.DirectoryEntry IISAdmin;
            System.DirectoryServices.DirectoryEntry VDir;
            bool IISUnderNT;
            // 
            // 确定IIS版本
            //
            IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");
            if(IISSchema.Properties["Syntax"].Value.ToString().ToUpper()=="BOOLEAN")
                IISUnderNT=true;
            else
                IISUnderNT=false;
            IISSchema.Dispose();
            // 
            // Get the admin object
            // 获得管理权限
            //
            IISAdmin=new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root");

           if (IISAdmin == null)
               
return "IIS 未正常安装";
          
if (IISAdmin.Children == null)
               
return "IIS 可能未启动";

            // 
            // If we're not creating a root directory
            // 如果我们不能创建一个根目录
            //
            if (!RootDir)
            {
                //
                // If the virtual directory already exists then delete it
                // 如果虚拟目录已经存在则删除
                //
                foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children) 
                {
                    if (v.Name == VDirName)
                    {
                        // Delete the specified virtual directory if it already exists
                        try
                        {
                        IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName });
                        IISAdmin.CommitChanges();
                        }
                        catch(Exception ex)
                        {
                        sRet+=ex.Message;
                        }
                    }
                }
            }  
            // 
            // Create the virtual directory
            // 创建一个虚拟目录
            //
            if (!RootDir)
            {
                VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
            }
            else
            {
                VDir = IISAdmin;
            }

            //
            // Make it a web application
            // 创建一个web应用
            //
            VDir.Properties["Path"][0] = Path; //设置虚拟目录指向的物理路径
            if (IISUnderNT)
            {
                VDir.Invoke("AppCreate", false);
            }
            else
            {
                VDir.Invoke("AppCreate", 1);
            }
            // 
            // Setup the VDir
            // 设置虚拟目录
            //
            VDir.Properties["AccessRead"][0] = chkRead; //设置读取权限
            VDir.Properties["AccessExecute"][0] = chkExecute; //设置执行权限
            VDir.Properties["AccessWrite"][0] = chkWrite; //设置写入权限
            VDir.Properties["AccessScript"][0] = chkScript; //执行权限
            VDir.Properties["DefaultDoc"][0] = "index.asp,Default.aspx";//设置默认文档,多值情况下中间用逗号分割
            VDir.Properties["AppFriendlyName"][0] = VDirName; //应用程序名称
            VDir.Properties["AuthFlags"][0] = 0;   //    设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
            VDir.Properties["AuthNTLM"][0] = chkAuth;
            VDir.Properties["EnableDefaultDoc"][0] = true;
            VDir.Properties["EnableDirBrowsing"][0] = false;
            // 
            // NT doesn't support this property
            // NT格式不支持这特性
            //
            if (!IISUnderNT)
            {
                VDir.Properties["AspEnableParentPaths"][0] = true;
            }
            // 
            // Set the changes
            // 设置改变
            //
            VDir.CommitChanges();

           //下面的方法是得到所有属性名称的方法:
           foreach (PropertyValueCollection pvc in  VDir.Properties)
           {
               Console.WriteLine(pvc.PropertyName);
            }
            sRet+= "VRoot " +VDirName + " created!"; 
            return sRet;
        }

        #region Properties
        public string ServerName
        {
        get
        {
        return _serverName;
        }
        set
        {
        _serverName = value;
        }
        }
        #endregion
        public static string VirDirSchemaName = "IIsWebVirtualDir";
        #region Private Members   
        private string _serverName;
        #endregion 
    }
}

测试用:

MessageBox.Show(new IISManager().CreateVDir("localhost", "ietm", "c:\\myweb", false, true, false, false, true, false, 1, "localhost"));
这个我已投入项目中使用,可放心使用。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoquwan.com 版权所有 湘ICP备2024080961号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务