首页 > CSharp > 使用C#调用外部命令重启IIS应用程序池

使用C#调用外部命令重启IIS应用程序池

2009年5月4日

在这里我们通过.net的Process类调用外部命令, 通过重定向输入, 输出获取执行结果.
添加应用using System.Diagnostics;
核心代码如下

                                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.WriteLine(_command);
                                p.StandardInput.WriteLine("exit");
                                //string command = "cscript.exe";
                                //p.StartInfo.Arguments = command + " c:\\windows\\system32\\iisapp.vbs /a \"www.xxx.net\" ";
 
                                //p.WaitForExit();
                                string s = p.StandardOutput.ReadToEnd();
                                p.Close();
                                LogEvent(s, LogType.normal);

_command写到配置文件中, 内容是

<add key="Command" value="cscript.exe c:\\windows\\system32\\iisapp.vbs /a www.xxx.net" />

这样就可以简单的实现重新启动指定的应用程序池了.
您可以通过命令iisapp -a来查看当前服务器上的所有应用程序.

最后可以根据自己的需要把这个代码写成windows服务, 安装到服务器上. 既可实现定时重新启动应用程序池功能.

admin CSharp

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.