用asp.net来回收IIS6.0应用程序池

网站由于使用比较多的缓存,有时会出现缓存错误,导致程序异常,这个时候需要回收一个应用程序池就可以了。
以前每次都是通过远程桌面连接到服务器,然后在应用程序池上右击》回收,感觉比较麻烦,于时找了如下的一个办法来实现。
其实就是普通的一个asp.net页面,加上一个按钮,进行回收,主要程序如下:
 1 
 2       protected   void  StartStopRecycleApp( string  method)
 3      {
 4           string  AppPoolName  =   this .tbAppName.Text.Trim();
 5           // string method = "Recycle";
 6 
 7           try
 8          {
 9              DirectoryEntry appPool  =   new  DirectoryEntry( " IIS://localhost/W3SVC/AppPools " );
10              DirectoryEntry findPool  =  appPool.Children.Find(AppPoolName,  " IIsApplicationPool " );
11              findPool.Invoke(method,  null );
12              appPool.CommitChanges();
13              appPool.Close();
14              lbMsg.Text  =   string .Format( " 应用程序池{0}{1}成功 " , AppPoolName,method);
15          }
16           catch  (Exception ex)
17          {
18              lbMsg.Text  =   string .Format( " 应用程序池{0}{2}失败:{1} " , AppPoolName, ex.Message,method);
19          }
20      }

tbAppName是一个textbox,用来输入应用程序池的名字,如“DefaultAppPool”。
当method="Recycle"时就是回收,为“Start”时是启动,为“Stop”时是停止。

注意:
1. 必须引入System.DirectoryServices包
2. 运行此程序的应用程序也的用户必须权限比较高,可以单独为此程序提供应用程序程,或者建立一个虚拟目录在配制里模拟高级用户(如administrators或者system),否则应用程序会抛出“拒绝访问”的异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值