当前位置: 移动技术网 > IT编程>开发语言>.net > .net重启iis线程池和iis站点程序代码分享

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

温州广电被黑,绵阳博文招聘,2626小游戏

重启站点:

复制代码 代码如下:

 /// <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;
        }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网