用C#+WMI實現擷取w3wp進程對應的應用程式集區

來源:互聯網
上載者:User
   自從用了ASP.NET2.0以後,這個問題被漸漸關注起來,目前的方法就是調用iisapp.vbs擷取。
  今天準備在我的文本轉換工具裡整合這個功能,於是,用C#實現了一下。
  
  using System;
  using System.Text;
  using System.Text.RegularExpressions;
  using System.Diagnostics;
  using System.Management;
  using System.Windows.Forms;
  
  namespace TextConvertor
  {
   /**//// <summary>
   /// W3wp 的摘要說明。
   /// </summary>
   public class W3wp
   {
   private W3wp(){}
   public static string GetAllW3wp(string input)
   {
   ObjectQuery oQuery = new ObjectQuery("select * from Win32_Process where Name='w3wp.exe'");
   ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);
   ManagementObjectCollection oreturnCollection = oSearcher.Get();
  
   string pid;
   string cmdLine;
   StringBuilder sb = new StringBuilder() ;
   foreach(ManagementObject oreturn in oreturnCollection)
   {
   pid = oreturn.GetPropertyValue("ProcessId").ToString();
   cmdLine = (string)oReturn.GetPropertyValue("CommandLine");
  
   string pattern = "-ap \"(.*)\"" ;
   Regex regex = new Regex(pattern, RegexOptions.IgnoreCase) ;
   Match match = regex.Match(cmdLine) ;
   string appPoolName = match.Groups[1].ToString() ;
   sb.AppendFormat("W3WP.exe PID: {0} AppPoolId:{1}\r\n", pid, appPoolName );
   }
  
   return sb.ToString();
   }
   }
  }
  
  實現的原理和VBScript簡直一模一樣。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.