当前位置: 移动技术网 > IT编程>开发语言>c# > C#操作IIS程序池及站点的创建配置实现代码

C#操作IIS程序池及站点的创建配置实现代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
首先要对microsoft.web.administration进行引用,它主要是用来操作iis7; using system.directoryservices;usi

首先要对microsoft.web.administration进行引用,它主要是用来操作iis7;

using system.directoryservices;
using microsoft.web.administration;

1:首先是对本版iis的版本进行配置:

复制代码 代码如下:

directoryentry getentity = new directoryentry("iis://localhost/w3svc/info");
            string version = getentity.properties["majoriisversionnumber"].value.tostring();
            messagebox.show("iis版本为:" + version);

2:是判断程序池是存在;

复制代码 代码如下:

/// <summary>
        /// 判断程序池是否存在
        /// </summary>
        /// <param name="apppoolname">程序池名称</param>
        /// <returns>true存在 false不存在</returns>
        private bool isapppoolname(string apppoolname)
        {
            bool result = false;
            directoryentry apppools = new directoryentry("iis://localhost/w3svc/apppools");
            foreach (directoryentry getdir in apppools.children)
            {
                if (getdir.name.equals(apppoolname))
                {
                    result = true;
                }
            }
            return result;
        }

3:删除应用程序池

复制代码 代码如下:

/// <summary>
        /// 删除指定程序池
        /// </summary>
        /// <param name="apppoolname">程序池名称</param>
        /// <returns>true删除成功 false删除失败</returns>
        private bool deleteapppool(string apppoolname)
        {
            bool result = false;
            directoryentry apppools = new directoryentry("iis://localhost/w3svc/apppools");
            foreach (directoryentry getdir in apppools.children)
            {
                if (getdir.name.equals(apppoolname))
                {
                    try
                    {
                        getdir.deletetree();
                        result = true;
                    }
                    catch
                    {
                        result = false;
                    }
                }
            }
            return result;
        }

4:创建应用程序池 (对程序池的设置主要是针对iis7;iis7应用程序池托管模式主要包括集成跟经典模式,并进行net版本的设置)

复制代码 代码如下:

string apppoolname = "lamapppool";
            if (!isapppoolname(apppoolname))
            {
                directoryentry newpool;
                directoryentry apppools = new directoryentry("iis://localhost/w3svc/apppools");
                newpool = apppools.children.add(apppoolname, "iisapplicationpool");
                newpool.commitchanges();
                messagebox.show(apppoolname + "程序池增加成功");
            }
            #endregion

            #region 修改应用程序的配置(包含托管模式及其net运行版本)
            servermanager sm = new servermanager();
            sm.applicationpools[apppoolname].managedruntimeversion = "v4.0";
            sm.applicationpools[apppoolname].managedpipelinemode = managedpipelinemode.classic; //托管模式integrated为集成 classic为经典
            sm.commitchanges();
            messagebox.show(apppoolname + "程序池托管管道模式:" + sm.applicationpools[apppoolname].managedpipelinemode.tostring() + "运行的net版本为:" + sm.applicationpools[apppoolname].managedruntimeversion);

运用c#代码来对iis7程序池托管管道模式及版本进行修改;



5:针对iis6的net版进行设置;因为此处我是用到net4.0所以v4.0.30319 若是net2.0则在这进行修改 v2.0.50727

复制代码 代码如下:

//启动aspnet_regiis.exe程序
            string filename = environment.getenvironmentvariable("windir") + @"\microsoft.net\framework\v4.0.30319\aspnet_regiis.exe";
            processstartinfo startinfo = new processstartinfo(filename);
            //处理目录路径
            string path = vdentry.path.toupper();
            int index = path.indexof("w3svc");
            path = path.remove(0, index);
            //启动aspnet_iis.exe程序,刷新脚本映射
            startinfo.arguments = "-s " + path;
            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();

6:平常我们可能还得对iis中的mime类型进行增加;下面主要是我们用到两个类型分别是:xaml,xap

复制代码 代码如下:

iisole.mimemapclass newmime = new iisole.mimemapclass();
            newmime.extension = ".xaml"; newmime.mimetype = "application/xaml+xml";
            iisole.mimemapclass twomime = new iisole.mimemapclass();
            twomime.extension = ".xap"; twomime.mimetype = "application/x-silverlight-app";
            rootentry.properties["mimemap"].add(newmime);
            rootentry.properties["mimemap"].add(twomime);
            rootentry.commitchanges();

7:下面是做安装时一段对iis进行操作的代码;兼容iis6及iis7;新建虚拟目录并对相应的属性进行设置;对iis7还进行新建程序池的程序;并设置程序池的配置;

复制代码 代码如下:

/// <summary>
    /// 创建网站
    /// </summary>
    /// <param name="siteinfo"></param>
      public  void createnewwebsite(newwebsiteinfo siteinfo)
        {
            if (!ensurenewsiteenavaible(siteinfo.bindstring))
            {
                throw new exception("该网站已存在" + environment.newline + siteinfo.bindstring);
            }
            directoryentry rootentry = getdirectoryentry(entpath);

            newsitenum = getnewwebsiteid();
            directoryentry newsiteentry = rootentry.children.add(newsitenum, "iiswebserver");
            newsiteentry.commitchanges();

            newsiteentry.properties["serverbindings"].value = siteinfo.bindstring;
            newsiteentry.properties["servercomment"].value = siteinfo.commentofwebsite;
            newsiteentry.commitchanges();
            directoryentry vdentry = newsiteentry.children.add("root", "iiswebvirtualdir");
            vdentry.commitchanges();
            string changwebpath = siteinfo.webpath.trim().remove(siteinfo.webpath.trim().lastindexof('\\'),1);
            vdentry.properties["path"].value = changwebpath;


            vdentry.invoke("appcreate", true);//创建应用程序

            vdentry.properties["accessread"][0] = true; //设置读取权限
            vdentry.properties["accesswrite"][0] = true;
            vdentry.properties["accessscript"][0] = true;//执行权限
            vdentry.properties["accessexecute"][0] = false;
            vdentry.properties["defaultdoc"][0] = "login.aspx";//设置默认文档
            vdentry.properties["appfriendlyname"][0] = "labmanager"; //应用程序名称          
            vdentry.properties["authflags"][0] = 1;//0表示不允许匿名访问,1表示就可以3为基本身份验证,7为windows继承身份验证
            vdentry.commitchanges();

            //操作增加mime
            //iisole.mimemapclass newmime = new iisole.mimemapclass();
            //newmime.extension = ".xaml"; newmime.mimetype = "application/xaml+xml";
            //iisole.mimemapclass twomime = new iisole.mimemapclass();
            //twomime.extension = ".xap"; twomime.mimetype = "application/x-silverlight-app";
            //rootentry.properties["mimemap"].add(newmime);
            //rootentry.properties["mimemap"].add(twomime);
            //rootentry.commitchanges();

            #region 针对iis7
            directoryentry getentity = new directoryentry("iis://localhost/w3svc/info");
            int version =int.parse(getentity.properties["majoriisversionnumber"].value.tostring());
            if (version > 6)
            {
                #region 创建应用程序池
                string apppoolname = "labmanager";
                if (!isapppoolname(apppoolname))
                {
                    directoryentry newpool;
                    directoryentry apppools = new directoryentry("iis://localhost/w3svc/apppools");
                    newpool = apppools.children.add(apppoolname, "iisapplicationpool");
                    newpool.commitchanges();
                }
                #endregion

                #region 修改应用程序的配置(包含托管模式及其net运行版本)
                servermanager sm = new servermanager();
                sm.applicationpools[apppoolname].managedruntimeversion = "v4.0";
                sm.applicationpools[apppoolname].managedpipelinemode = managedpipelinemode.classic; //托管模式integrated为集成 classic为经典
                sm.commitchanges();
                #endregion

                vdentry.properties["apppoolid"].value = apppoolname;
                vdentry.commitchanges();
            }
            #endregion


            //启动aspnet_regiis.exe程序
            string filename = environment.getenvironmentvariable("windir") + @"\microsoft.net\framework\v4.0.30319\aspnet_regiis.exe";
            processstartinfo startinfo = new processstartinfo(filename);
            //处理目录路径
            string path = vdentry.path.toupper();
            int index = path.indexof("w3svc");
            path = path.remove(0, index);
            //启动aspnet_iis.exe程序,刷新脚本映射
            startinfo.arguments = "-s " + path;
            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);
            }

        }

复制代码 代码如下:

string entpath = string.format("iis://{0}/w3svc", "localhost");

public  directoryentry getdirectoryentry(string entpath)
       {
           directoryentry ent = new directoryentry(entpath);
           return ent;
       }

        public class newwebsiteinfo
        {
            private string hostip;   // 主机ip
            private string portnum;   // 网站端口号
            private string descofwebsite; // 网站表示。一般为网站的网站名。例如"www.dns.com.cn"
            private string commentofwebsite;// 网站注释。一般也为网站的网站名。
            private string webpath;   // 网站的主目录。例如"e:\ mp"

            public newwebsiteinfo(string hostip, string portnum, string descofwebsite, string commentofwebsite, string webpath)
            {
                this.hostip = hostip;
                this.portnum = portnum;
                this.descofwebsite = descofwebsite;
                this.commentofwebsite = commentofwebsite;
                this.webpath = webpath;
            }

            public string bindstring
            {
                get
                {
                    return string.format("{0}:{1}:{2}", hostip, portnum, descofwebsite); //网站标识(ip,端口,主机头值)
                }
            }

            public string portnum
            {
                get
                {
                    return portnum;
                }
            }

            public string commentofwebsite
            {
                get
                {
                    return commentofwebsite;
                }
            }

            public string webpath
            {
                get
                {
                    return webpath;
                }
            }
        }

8:下面的代码是对文件夹权限进行设置,下面代码是创建everyone 并给予全部权限

复制代码 代码如下:

/// <summary>
        /// 设置文件夹权限 处理给everone赋予所有权限
        /// </summary>
        /// <param name="fileadd">文件夹路径</param>
        public void setfilerole()
        {
            string fileadd = this.context.parameters["installdir"].tostring();
            fileadd = fileadd.remove(fileadd.lastindexof('\\'), 1);
            directorysecurity fsec = new directorysecurity();
            fsec.addaccessrule(new filesystemaccessrule("everyone",filesystemrights.fullcontrol,inheritanceflags.containerinherit|inheritanceflags.objectinherit,propagationflags.none,accesscontroltype.allow));
            system.io.directory.setaccesscontrol(fileadd, fsec);
        }

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网