我现在可以用C#实现查看w3wp进程的各模块的信息和相关进程的状态,
有没有办法能够实现查看时打开什么页面时调用的w3wp进程,以及页面调用时使用的内存以及CPU占用情况

解决方案 »

  1.   

    这个需要调用API,我给你找找去,
    我记得我以前写过的
      

  2.   


    //===========================声明API========================================
    [DllImport("kernel32")]
    public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
    [DllImport("kernel32")]
    public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
    [DllImport("kernel32")]
    public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
    [DllImport("kernel32")]
    public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
    [DllImport("kernel32")]
    public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
    //============================================================================
    //================定义CPU的信息结构===========================================
    [StructLayout(LayoutKind.Sequential)]
    public struct CPU_INFO
    {
    public uint dwOemId;//CPU的OEM
    public uint dwPageSize;//CPU的页面大小
    public uint lpMinimumApplicationAddress;
    public uint lpMaximumApplicationAddress;
    public uint dwActiveProcessorMask;
    public uint dwNumberOfProcessors;//CPU的个数
    public uint dwProcessorType;//CPU的类型
    public uint dwAllocationGranularity;
    public uint dwProcessorLevel;//CPU的等级
    public uint dwProcessorRevision;
    }
    //定义内存的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct MEMORY_INFO
    {
    public uint dwLength;
    public uint dwMemoryLoad;//正在使用的内存
    public uint dwTotalPhys;//全部内存的大小
    public uint dwAvailPhys;//可使用的物理内存的大小
    public uint dwTotalPageFile;//交换文件总大小
    public uint dwAvailPageFile;//尚可交换文件大小
    public uint dwTotalVirtual;//总虚拟内存
    public uint dwAvailVirtual;//剩余虚拟内存
    }
    //定义系统时间的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME_INFO
    {
    public ushort wYear;//年
    public ushort wMonth;//月
    public ushort wDayOfWeek;
    public ushort wDay;//日
    public ushort wHour;//时
    public ushort wMinute;//分
    public ushort wSecond;//秒
    public ushort wMilliseconds;
    }
    //=========================================================================================
                                    string ProcessName=进行名称
    Process[] MyProcess=Process.GetProcessesByName(ProcessName);
    this.label26.Text="进程镜像名:"+MyProcess[0].ProcessName;
    this.label27.Text="进程ID;"+MyProcess[0].Id;
    this.label29.Text="启动线程数:"+MyProcess[0].Threads.Count.ToString();
    this.label28.Text="CPU占用时间:"+MyProcess[0].TotalProcessorTime.ToString();
    this.label30.Text="线程优先级:"+MyProcess[0].PriorityClass.ToString();
    this.label31.Text="启动时间:"+MyProcess[0].StartTime.ToLongTimeString();
    this.label32.Text="专用内存:"+(MyProcess[0].PrivateMemorySize/1024).ToString()+"K";
    this.label33.Text="峰顶虚拟内存:"+(MyProcess[0].PeakVirtualMemorySize/1024).ToString()+"K";
    this.label35.Text="峰顶分页内存:"+(MyProcess[0].PeakPagedMemorySize/1024).ToString()+"K";
    this.label34.Text="分页系统内存:"+(MyProcess[0].PagedSystemMemorySize/1024).ToString()+"K";
    this.label36.Text="分页内存:"+(MyProcess[0].PagedMemorySize/1024).ToString()+"K";
    this.label37.Text="未分页系统内存:"+(MyProcess[0].NonpagedSystemMemorySize/1024).ToString()+"K";
    this.label38.Text="物理内存:"+(MyProcess[0].WorkingSet/1024).ToString()+"K";
    this.label39.Text="虚拟内存:"+(MyProcess[0].VirtualMemorySize/1024).ToString()+"K";好了,我只不过是将以前做过的这部分复制了下来,你看看,有什么不明白的地方再说
      

  3.   

    最下面的那些统计你看看你想放到按钮里或哪里都行
    同时要记得倒入using System.Runtime.InteropServices;//调用API所用到的命名空间
    using System.Text;//在用到String Builder时用到
      

  4.   

    用Proccess类就行。
    下面的示例检索的信息涉及当前进程、本地计算机上运行的“记事本”的所有实例、在使用计算机别名和 IP 地址的特定计算机上运行的“记事本”的所有实例、本地计算机和远程计算机上运行的所有进程,以及本地计算机或远程计算机上使用进程 ID 的特定进程。using System;
    using System.Diagnostics;
    using System.ComponentModel;namespace MyProcessSample
    {
    /// <summary>
    /// Shell for the sample.
    /// </summary>
    class MyProcess
    {

       

    void BindToRunningProcesses()
    {
    // Get the current process.
    Process currentProcess = Process.GetCurrentProcess();
    // Get all instances of Notepad running on the local
    // computer.
    Process [] localByName = Process.GetProcessesByName("notepad");
    // Get all instances of Notepad running on the specifiec
    // computer.
    // 1. Using the computer alias (do not precede with "\\").
    Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

    // 2. Using an IP address to specify the machineName parameter. 
    Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");


    // Get all processes running on the local computer.
    Process [] localAll = Process.GetProcesses();
    // Get all processes running on the remote computer.
    Process [] remoteAll = Process.GetProcesses("myComputer");
    // Get a process on the local computer, using the process id.
    Process localById = Process.GetProcessById(1234);
    // Get a process on a remote computer, using the process id.
    Process remoteById = Process.GetProcessById(2345, "myComputer");

    }

    static void Main()
    {
                    
                MyProcess myProcess = new MyProcess();
    myProcess.BindToRunningProcesses();         }
    }
    }
      

  5.   

    string ProcessName=进行名称
    这怎么成这样了
    应该是
    string ProcessName=你所想要查看的进程名称;
      

  6.   

    public static Process[] GetProcessesByName (
    string processName
    )
    /*
    参数
    processName
    该进程的友好名称。 返回值
    Process 类型的数组,它表示运行指定应用程序或文件的进程资源。
    */
    使用此方法创建新的 Process 组件的数组,并将它们与本地计算机上运行同一可执行文件的所有进程资源关联。该进程资源必须已经存在于计算机上,因为 GetProcessesByName 不创建系统资源,而是将其与应用程序生成的 Process 组件关联。可以为当前不在本地计算机上运行的可执行文件指定 processName,因此该方法返回的数组可以为空。该进程名是不包括 .exe 扩展名或路径的进程友好名称,如 Outlook。GetProcessesByName 对于获取和操作与同一可执行文件关联的所有进程非常有用。例如,可以将可执行文件名作为 processName 参数传递,以便关闭该可执行文件的所有运行中的实例。虽然进程 Id 对于系统上的单个进程资源是唯一的,但是本地计算机上的多个进程可以运行由 processName 参数指定的应用程序。因此,GetProcessById 最多返回一个进程,但 GetProcessesByName 返回包含所有关联进程的数组。如果需要使用标准 API 调用操作进程,则可以依次查询这些进程中的每一个的标识符。无法仅通过进程名访问进程资源,但是检索到与进程资源关联的 Process 组件数组后,就可以启动、终止和以其他方式操作系统资源了。
      

  7.   

    using System;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;namespace ProcessList
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    write_log(dateInf());
    write_log("============w3wp Process Detail List============");
    try
    {
    Process[] newProcess=Process.GetProcessesByName("w3wp","usd-t");
    foreach(Process prc in newProcess)
    {
    write_log("------------w3wp info------------");
    write_log("ProcessName:\t\t"+prc.ProcessName.ToString());
    write_log("ProcessID(PID):\t\t"+prc.Id.ToString());
    write_log("ThreadCount:\t\t"+prc.Threads.Count.ToString());
    write_log("MainModuleFileName:\t"+prc.MainModule.FileName.ToString());
    write_log("TotalProcessorTime:\t"+prc.TotalProcessorTime.TotalSeconds.ToString()+"s");
    write_log("StartInfo_FileName:\t"+prc.StartInfo.FileName.ToString());
    write_log("StartInfo_WorkingDirectory:\t"+prc.StartInfo.WorkingDirectory.ToString()+"\n");
    ProcessThreadCollection ThdCollecion=prc.Threads; int ixx=0;
                        foreach(System.Diagnostics.ProcessThread PThd in ThdCollecion)
    {
    write_log("Thread "+PThd.Id+" ThreadState:\t"+PThd.ThreadState);
    write_log("Thread "+PThd.Id+" WaitReason:\t"+PThd.WaitReason);
    write_log("Thread "+PThd.Id+" TotalSeconds:\t"+PThd.UserProcessorTime.TotalSeconds+"s");
    ixx+=1;
    }
    System.Console.WriteLine("\n");
    for(int ix=0;ix<prc.Modules.Count;ix++)
    {
    ProcessModule pm=prc.Modules[ix];
    write_log("Modules "+ix+" FileName:\t"+pm.FileName.ToString());
    write_log("ModuleMemorySize:\t"+pm.ModuleMemorySize.ToString());
    }
    }
    //System.Console.WriteLine("============w3wp Process Detail List============");
    // 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; 
    // string test;
    // //StringBuilder sb = new StringBuilder() ; 
    // foreach(ManagementObject oReturn in oReturnCollection) 
    // { 
    // pid = oReturn.GetPropertyValue("ProcessId").ToString(); 
    // cmdLine = (string)oReturn.GetPropertyValue("CommandLine");
    // test=oReturn.GetText(System.Management.TextFormat.WmiDtd20);
    //   
    // string pattern = "-ap \"(.*)\"" ; 
    // Regex regex = new Regex(pattern, RegexOptions.IgnoreCase) ; 
    // Match match = regex.Match(cmdLine) ; 
    // string appPoolName = match.Groups[1].ToString() ; 
    // System.Console.WriteLine("W3WP.exe PID: {0} AppPoolId:{1}\r\n", pid, appPoolName );
    // System.Console.WriteLine(test);
    // }
    System.Console.Read();
    }
    catch(Exception ex)
    {
    System.Console.WriteLine("Error Info:");
    System.Console.WriteLine(ex.Message+"\n"+ex.StackTrace);
    System.Console.Read();
    return;
    }
    } public static string dateInf()
    {
    int intMonthInf=Convert.ToInt32(DateTime.Now.Month);
    string MonthInf=string.Empty;
    if(intMonthInf<10)
    {
    MonthInf="0"+intMonthInf.ToString();
    }
    else
    {
    MonthInf=intMonthInf.ToString();
    }
    int intDayInf=Convert.ToInt32(DateTime.Now.Day);
    string DayInf=string.Empty;
    if(intDayInf<10)
    {
    DayInf="0"+intDayInf.ToString();
    }
    else
    {
    DayInf=intDayInf.ToString();
    }
    int intHourInf=Convert.ToInt32(DateTime.Now.Hour);
    string HourInf=string.Empty;
    if(intDayInf<10)
    {
    HourInf="0"+intDayInf.ToString();
    }
    else
    {
    HourInf=intDayInf.ToString();
    }
    int intMinInf=Convert.ToInt32(DateTime.Now.Minute);
    string MinInf=string.Empty;
    if(intMinInf<10)
    {
    MinInf="0"+intMinInf.ToString();
    }
    else
    {
    MinInf=intMinInf.ToString();
    }
    string YearInf=DateTime.Now.Year.ToString(); return YearInf+"_"+MonthInf+"_"+DayInf+"_"+HourInf+"_"+MinInf;
    } public static string write_log(string input_log)
    {
    string back=string.Empty;
    string Flag=string.Empty;

    string baseLogPath="e:\\";
    DirectoryInfo LogFile=new DirectoryInfo(baseLogPath);
    string logFilePath=baseLogPath+"w3wp_detail_"+dateInf();
    logFilePath+=".txt";
    FileInfo newLogFile=new FileInfo(logFilePath); //判断LogFileMonth目录中是否含有newLogFile文件
    //如果没有该文件就创建该文件
    if(newLogFile.Exists==false)
    {
    try
    {
    StreamWriter fw = newLogFile.CreateText();
    fw.WriteLine(input_log);
    fw.Flush();
    fw.Close();
    }
    catch(Exception fie)
    {
    return fie.ToString();
    }
    return "";
    }
    else
    {
    try
    {
    StreamWriter fw = File.AppendText(newLogFile.ToString());
    fw.WriteLine(input_log);
    fw.Flush();
    fw.Close();
    }
    catch(Exception fie)
    {
    return fie.ToString();
    }
    return "";
    }
    }
    }
    }
    这是写的代码
    他只能开到线程的状态,有没有办法看到线程的详细一点的信息
      

  8.   

    Process类中几个你可以感兴趣的属性:
      名称  说明  
      BasePriority  获取关联进程的基本优先级。 
      Container   获取 IContainer,它包含 Component。(从 Component 继承。) 
       EnableRaisingEvents  获取或设置在进程终止时是否应激发 Exited 事件。 
       ExitCode  获取关联进程终止时指定的值。 
      ExitTime  获取关联进程退出的时间。 
      Handle  返回关联进程的本机句柄。 
      HandleCount  获取由进程打开的句柄数。 
       HasExited  获取指示关联进程是否已终止的值。 
       Id  获取关联进程的唯一标识符。 
      MachineName  获取关联进程正在其上运行的计算机的名称。 
      MainModule  获取关联进程的主模块。 
       MainWindowHandle  获取关联进程主窗口的窗口句柄。 
      MainWindowTitle  获取进程的主窗口标题。 
      MaxWorkingSet  获取或设置关联进程的允许的最大工作集大小。 
      MinWorkingSet  获取或设置关联进程的允许的最小工作集大小。 
      Modules  获取已由关联进程加载的模块。 
      NonpagedSystemMemorySize  获取分配给此进程的未分页的系统内存大小。 
      NonpagedSystemMemorySize64  获取为关联的进程分配的非分页系统内存量。 
      PagedMemorySize  获取分页的内存大小。 
      PagedMemorySize64  获取为关联的进程分配的分页内存量。 
      PagedSystemMemorySize  获取分页的系统内存大小。 
      PagedSystemMemorySize64  获取为关联的进程分配的可分页系统内存量。 
      PeakPagedMemorySize  获取峰值分页内存大小。 
      PeakPagedMemorySize64  获取关联的进程使用的虚拟内存分页文件中的最大内存量。 
      PeakVirtualMemorySize  获取峰值虚拟内存大小。 
      PeakVirtualMemorySize64  获取关联的进程使用的最大虚拟内存量。 
      PeakWorkingSet  获取关联进程的峰值工作集大小。 
      PeakWorkingSet64  获取关联的进程使用的最大物理内存量。 
      PriorityBoostEnabled  获取或设置一个值,该值指示主窗口拥有焦点时是否由操作系统暂时提升关联进程的优先级。 
      PriorityClass  获取或设置关联进程的总体优先级类别。 
      PrivateMemorySize  获取专用内存大小。 
      PrivateMemorySize64  获取为关联的进程分配的专用内存量。 
      PrivilegedProcessorTime  获取此进程的特权处理器时间。 
      ProcessName  获取该进程的名称。 
      ProcessorAffinity  获取或设置可安排此进程中的线程在其上运行的处理器。 
       Responding  获取指示进程的用户界面当前是否响应的值。 
      SessionId  获取关联的进程的终端服务会话标识符。 
       Site   获取或设置 Component 的 ISite。(从 Component 继承。) 
      StandardError  获取用于读取应用程序错误输出的流。 
      StandardInput  获取用于写入应用程序输入的流。 
      StandardOutput  获取用于读取应用程序输出的流。 
       StartInfo  获取或设置要传递给 Process 的 Start 方法的属性。 
      StartTime  获取关联进程启动的时间。 
      SynchronizingObject  获取或设置用于封送由于进程退出事件而发出的事件处理程序调用的对象。 
      Threads  获取在关联进程中运行的一组线程。 
      TotalProcessorTime  获取此进程的总的处理器时间。 
      UserProcessorTime  获取此进程的用户处理器时间。 
      VirtualMemorySize  获取进程的虚拟内存大小。 
      VirtualMemorySize64  获取为关联的进程分配的虚拟内存量。 
      WorkingSet  获取关联进程的物理内存使用情况。 
      WorkingSet64  获取为关联的进程分配的物理内存量。 
      

  9.   

    呵,周公的确实做到了对相关进程的查询
    但是无法查询内存等的详细信息啊
    简单的查询进程来个foreach就可得到的
    例如获得当前所有进程 Process[] MyProcesses=Process.GetProcesses();
    foreach( Process MyProcess in MyProcesses)
    {
    this.listBox1.Items.Add(MyProcess.ProcessName);
    }还要倒入
    using System.Diagnostics;//操作进程
      

  10.   

    看了上面的讨论都是基于OS级别的,呵呵,很通用的,但是IIS自己的性能部分一般是不能简单用这样的方式代替的。IIS 7的话,lz可以考虑用system32\inetsrv\Microsoft.Web.Administrator.dll公开的接口来管理和监控IIS性能,这自然也包括w3wp.exe的行为。http://learn.iis.net/page.aspx/165/how-to-use-microsoftwebadministration/rev/2
    http://blogs.iis.net/mailant/archive/2008/01/10/new-worker-process-performance-counters-in-iis7.aspx老一些的版本,也可以通过WMI提供的接口来查询,但是应该不如IIS 7提供的方式自然和强大了。