C# 修改IIS相关设置代码

来源:互联网 发布:怎么租淘宝店 编辑:程序博客网 时间:2024/06/06 13:20

  /// <summary>
        /// 从IIS中查找虚拟目录
        /// </summary>
        /// <param name="prj">虚拟目录名称</param>
        /// <returns></returns>
        public static System.DirectoryServices.DirectoryEntry FindVirDir( string VirDirName)
        {
            System.DirectoryServices.DirectoryEntry rlst = null;

            //当项目在IIS上打开时,要把URL转换为物理路径
            if (prjname.ToLower().IndexOf("http://") >= 0)
            {
       

                System.DirectoryServices.DirectoryEntry rootEntry = new System.DirectoryServices.DirectoryEntry("IIS://localhost/w3svc");


                int siteID = 1;
                bool isFound = false;
                //得到现有的站点标识
                foreach (System.DirectoryServices.DirectoryEntry entry in rootEntry.Children)
                {
                    if (entry.SchemaClassName == "IIsWebServer")
                    {
                        int ID = Convert.ToInt32(entry.Name);
                        //查找IIS的根目录
                        System.DirectoryServices.DirectoryEntry root = entry.Children.Find("root", "IIsWebVirtualDir");

                        if (ID >= siteID)
                        {

                            foreach (System.DirectoryServices.DirectoryEntry centry in root.Children)
                            {

                                if (centry.SchemaClassName == "IIsWebVirtualDir")
                                {
                                    if (centry.Properties["AppFriendlyName"].Value.ToString().ToLower() == VirDirName.ToLower())
                                    {
                                        rlst = centry;
                                        isFound = true;
                                        break;
                                    }
                                }

                            }
                            if (isFound)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                rlst = prjname;
            }
            return rlst;

        }

 

 

 

 

  //创建虚拟目录

 

   //新建虚拟目录的DirectoryEntry对象

    System.DirectoryServices.DirectoryEntry tbEntry = root.Children.Add("虚拟目录名称", root.SchemaClassName);


    tbEntry.Properties["Path"][0] =PATH;   //设置虚拟目录的本地物理路径;
    tbEntry.Invoke("AppCreate", true);   //设置是否要创建新的虚似目录
               
    tbEntry.Properties["AccessRead"][0] = true;   
    tbEntry.Properties["ContentIndexed"][0] = true; ;

 

    //设置虚拟目录默认首页
    tbEntry.Properties["DefaultDoc"][0] = "index.asp,Default.asp,Default.aspx,index.aspx,login.aspx,login.asp";
    tbEntry.Properties["AppFriendlyName"][0] = "虚拟目录名称"  ;   
    tbEntry.Properties["AccessScript"][0] = true;  //
    tbEntry.Properties["DontLog"][0] = true;
    tbEntry.Properties["AuthFlags"][0] = 0;
    tbEntry.Properties["AuthFlags"][0] = 1;
    tbEntry.Properties["AspEnableParentPaths"][0]=true;  //是否支持父路径
    tbEntry.CommitChanges();  //提交

 

 

原创粉丝点击