zoukankan      html  css  js  c++  java
  • C#在IIS中创建站点和虚拟目录(源码下载)

    首先需要引用
    using System.DirectoryServices;
    以下为创建IIS站点和虚拟目录的主要类代码(全部源码下载)< p>


    /// <summary>
    /// 获取新网站ID
    /// </summary>
    /// <returns></returns>

    private string GetNewWebSiteID()
    {
        
    int iWebSiteCount = 0;

        DirectoryEntry siteEntry 
    = new DirectoryEntry("IIS://localhost/w3svc");

        
    foreach (DirectoryEntry childEntry in siteEntry.Children)
        
    {
            
    if (childEntry.SchemaClassName == "IIsWebServer") iWebSiteCount++;
        }


        
    return (iWebSiteCount + 1).ToString();
    }

    /// <summary>
    /// 获取新站点端口,默认为当前最大端口号加一
    /// </summary>
    /// <returns></returns>

    private string GetNewSitePort()
    {
        
    int iDefault = 8000;

        DirectoryEntry siteEntry 
    = new DirectoryEntry("IIS://localhost/w3svc");

        
    foreach (DirectoryEntry childEntry in siteEntry.Children)
        
    {
            
    if (childEntry.SchemaClassName == "IIsWebServer")
            
    {
                
    if (childEntry.Properties["ServerBindings"].Value != null)
                
    {
                    
    string strSettings = childEntry.Properties["ServerBindings"].Value.ToString();

                    
    int iSettingPort = int.Parse(strSettings.Substring(strSettings.IndexOf(':'+ 1, (strSettings.LastIndexOf(':'- strSettings.IndexOf(':'- 1)));

                    iDefault 
    = iSettingPort > iDefault ? iSettingPort : iDefault;
                }

            }

        }


        
    return (iDefault + 1).ToString();
    }

    /// <summary>
    /// 判断网站是否已经存在
    /// </summary>
    /// <param name="strSiteName"></param>
    /// <returns></returns>

    private bool IsExist(string strSiteName)
    {
        
    bool blExist = false;

        DirectoryEntry siteEntry 
    = new DirectoryEntry("IIS://localhost/w3svc");

        
    foreach (DirectoryEntry childEntry in siteEntry.Children)
        
    {
            
    if (childEntry.SchemaClassName == "IIsWebServer")
            
    {
                
    if (childEntry.Properties["ServerComment"].Value != null)
                
    {
                    
    if (childEntry.Properties["ServerComment"].Value.ToString() == strSiteName) blExist = true;
                }

            }

        }


        
    return blExist;
    }

    /// <summary>
    /// 创建网站
    /// </summary>
    /// <param name="strPort"></param>
    /// <param name="strSiteName"></param>
    /// <param name="strFilePath"></param>
    /// <returns></returns>

    private DirectoryEntry CreateNewSite(string strSiteName, string strFilePath)
    {
        
    try
        
    {
            strSitePort 
    = GetNewSitePort();

            DirectoryEntry deyRoot 
    = new DirectoryEntry("IIS://localhost/w3svc");

            DirectoryEntry siteEntry 
    = deyRoot.Children.Add(GetNewWebSiteID(), "IIsWebServer");

            siteEntry.Properties[
    "ServerComment"].Value = strSiteName;

            siteEntry.Properties[
    "ServerBindings"].Value = String.Format("{0}:{1}:{2}""", strSitePort, "");

            siteEntry.Properties[
    "ServerAutoStart"].Value = true;

            
    //添加虚拟目录,网站本身没有路径,默认的路径即名称为Root的虚拟目录
            DirectoryEntry rootEntry = siteEntry.Children.Add("Root""IIsWebVirtualDir");
            rootEntry.Properties[
    "Path"].Value = strFilePath;                       //文件夹路径
            rootEntry.Properties["AccessRead"][0= true;                           //读取权限
            rootEntry.Properties["AccessExecute"][0= false;                       //执行(如ISAPI应用程序或CGI)
            rootEntry.Properties["AccessWrite"][0= true;                          //写入
            rootEntry.Properties["AccessScript"][0= true;                         //执行脚本(如ASP)
            rootEntry.Properties["EnableDirBrowsing"][0= true;                   //浏览
            rootEntry.Properties["DefaultDoc"][0= "UserLogin.aspx,Default.aspx";  //设置默认文档,多值情况下中间用逗号分割
            rootEntry.CommitChanges(); siteEntry.CommitChanges(); return rootEntry;
        }

        
    catch (Exception ex)
        
    {
            MessageBox.Show(ex.Message.ToString() 
    + "站点创建失败!"); return null;
        }

    }

    /// <summary>
    /// 创建虚拟目录
    /// </summary>
    /// <param name="strDirName">虚拟目录名称</param>
    /// <param name="strDirPath">虚拟目录路径</param>
    /// <param name="siteEntry">要创建虚拟目录的原站点</param>
    /// <returns></returns>

    private bool CreateVirtualDirectory(string strDirName, string strDirPath, DirectoryEntry rootEntry)
    {
        
    try
        
    {
            DirectoryEntry childEntry 
    = rootEntry.Children.Add(strDirName, "IIsWebVirtualDir");

            childEntry.Invoke(
    "AppCreate"true);
            childEntry.Properties[
    "Path"].Value = strDirPath;// @"D:\公司项目\盘县联网\SafetyProduce\SafetyNet\";        //文件夹路径
            childEntry.Properties["AppFriendlyName"][0= strDirName;//应用程序名称 
            childEntry.Properties["AccessRead"][0= true;           //读取权限
            childEntry.Properties["AccessExecute"][0= false;       //执行(如ISAPI应用程序或CGI)
            childEntry.Properties["AccessWrite"][0= true;          //写入
            childEntry.Properties["AccessScript"][0= true;         //执行脚本(如ASP)
            childEntry.Properties["EnableDirBrowsing"][0= true;   //浏览
            childEntry.Properties["DefaultDoc"][0= "UserLogin.aspx,Default.aspx"////设置默认文档,多值情况下中间用逗号分割

            childEntry.CommitChanges(); rootEntry.CommitChanges(); 
    return true;
        }

        
    catch (Exception ex)
        
    {
            MessageBox.Show(ex.Message.ToString() 
    + "虚拟目录" + strDirName + "创建失败!"); return false;
        }

    }

    以下为调用代码

    //创建站点
    DirectoryEntry rootEntry = CreateNewSite("天科行业主管平台", strDirectoryPath + "WebPage");

    if (rootEntry != null)
    {
        
    //创建虚拟目录
        CreateVirtualDirectory("SafetyNet", strDirectoryPath + "SafetyNet", rootEntry); 
        CreateVirtualDirectory(
    "SafetySupervise", strDirectoryPath + "SafetySupervise", rootEntry); 
    }

     全部源码下载

  • 相关阅读:
    Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树
    Red Black Tree java.util.TreeSet
    finding friends with mapreduce
    zabbix 监控jmx 需要--enable-java
    zabbix 监控jmx 需要--enable-java
    zabbix 监控jvm
    zabbix 监控jvm
    eval 捕获dbi错误
    eval 捕获dbi错误
    eval 捕获错误
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1232210.html
Copyright © 2011-2022 走看看