欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net重启iis线程池和iis站点程序代码分享

程序员文章站 2024-02-28 21:53:10
重启站点:复制代码 代码如下: ///         ///...

重启站点:

复制代码 代码如下:

 /// <summary>
        /// 根据名字重启站点.(没重启线程池)
        /// </summary>
        /// <param name="sitename"></param>
        static void restartwebsite(string sitename)
        {
            try
            {
                var server = new servermanager();
                var site = server.sites.firstordefault(s => s.name == sitename);
                if (site != null)
                {
                    site.stop();
                    if (site.state == objectstate.stopped)
                    {
                    }
                    else
                    {
                        console.writeline("could not stop website!");
                        throw new invalidoperationexception("could not stop website!");
                    }
                    site.start();
                }
                else
                {
                    console.writeline("could not find website!");

                    throw new invalidoperationexception("could not find website!");
                }

            }
            catch (exception e)
            {
                console.writeline(e);
            }
        }

        /// <summary>
        /// 重启完之后.要再检测下.是否开启了
        /// </summary>
        /// <param name="sitename"></param>
        static void fixwebsite(string sitename)
        {
            try
            {
                var server = new servermanager();
                var site = server.sites.firstordefault(s => s.name == sitename);
                if (site != null)
                {
                    if (site.state != objectstate.started)
                    {
                        thread.sleep(500);

                        //防止状态为正在开启
                        if (site.state != objectstate.started)
                        {
                            site.start();
                        }
                    }
                }
            }
            catch (exception e)
            {
                console.writeline(e);
            }
        }

重启iis线程池:

复制代码 代码如下:

/// <summary>
        /// 线程池名字
        /// </summary>
        /// <param name="name"></param>
        static void restartiispool(string name)
        {
            string[] cmds = { "c:", @"cd %windir%\system32\inetsrv", string.format("appcmd stop apppool /apppool.name:{0}", name), string.format("appcmd start apppool /apppool.name:{0}", name) };
            cmd(cmds);
            closeprocess("cmd.exe");
        }

        /// <summary>
        /// 运行cmd命令
        /// </summary>
        /// <param name="cmd">命令</param>
        /// <returns></returns>
        public static string cmd(string[] cmd)
        {
            process p = new process();
            p.startinfo.filename = "cmd.exe";
            p.startinfo.useshellexecute = false;
            p.startinfo.redirectstandardinput = true;
            p.startinfo.redirectstandardoutput = true;
            p.startinfo.redirectstandarderror = true;
            p.startinfo.createnowindow = true;
            p.start();
            p.standardinput.autoflush = true;
            for (int i = 0; i < cmd.length; i++)
            {
                p.standardinput.writeline(cmd[i]);
            }
            p.standardinput.writeline("exit");

            string strrst = p.standardoutput.readtoend();

            //debug.print(strrst);

            p.waitforexit();
            p.close();
            return strrst;
        }

        /// <summary>
        /// 关闭进程
        /// </summary>
        /// <param name="procname">进程名称</param>
        /// <returns></returns>
        public static bool closeprocess(string procname)
        {
            bool result = false;
            var proclist = new arraylist();
            foreach (process thisproc in process.getprocesses())
            {
                var tempname = thisproc.tostring();
                int begpos = tempname.indexof("(") + 1;
                int endpos = tempname.indexof(")");
                tempname = tempname.substring(begpos, endpos - begpos);
                proclist.add(tempname);
                if (tempname == procname)
                {
                    if (!thisproc.closemainwindow())
                        thisproc.kill(); // 当发送关闭窗口命令无效时强行结束进程
                    result = true;
                }
            }
            return result;
        }