设为首页 收藏本站
查看: 1002|回复: 0

[经验分享] C#在IIS中创建站点和虚拟目录(源码下载)

[复制链接]

尚未签到

发表于 2015-8-13 08:01:05 | 显示全部楼层 |阅读模式
  首先需要引用
using System.DirectoryServices;
以下为创建IIS站点和虚拟目录的主要类代码(全部源码下载)< p>

DSC0000.gif
DSC0001.gif DSC0002.gif /**//// <summary>
DSC0003.gif /// 获取新网站ID
/// </summary>
DSC0004.gif /// <returns></returns>
private string GetNewWebSiteID()
DSC0005.gif {
    int iWebSiteCount = 0;

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

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

    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);
}  全部源码下载

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-98163-1-1.html 上篇帖子: 修改IIS设置延长DEBUG时间 下篇帖子: 修改host文件实现自定义域名和iis站点本地调试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表