一个简单的进程察看器

using System;
using System.Diagnostics;

namespace ProcessManager
{
    
/// <summary>
    
/// Summary description for Class1.
    
/// </summary>

    class MainEntry
    
{
        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            
if(args.Length<1)ListProcesses();
            
else if(args[0].Length>0)ListProcess(args[0]);
        }


        
private static void ListProcesses()
        
{
            
string output = "";
            
int limit = 36;
            Console.WriteLine(
"PID".PadRight(6)+"Name".PadRight(16)+"Memory".PadLeft(10)+"VM size".PadLeft(10)+"Threads count".PadLeft(20));
            Console.WriteLine(
"-".PadRight(limit+34,'-'));
            
foreach(Process proc in Process.GetProcesses())
            
{
                
                
string pid = proc.Id.ToString();
                
string pname = proc.ProcessName;
                
string pcount = proc.Threads.Count.ToString();
                
string pmemsize = (proc.PeakWorkingSet/1024).ToString()+"K";
                
string pvmsize = (proc.PeakPagedMemorySize/1024).ToString()+"K";
                
                output 
= String.Format("{0}{1}{2}{3}{4}",pid.PadRight(6),pname.PadRight(16),pmemsize.PadLeft(10),pvmsize.PadLeft(10),pcount.PadLeft(10));
                Console.WriteLine(output);
            }

        }
         

        
private static void ListProcess(string processid)
        
{
            
int pid = -1;
            
try
            
{
                pid 
= Convert.ToInt32(processid);
            }

            
catch
            
{

            }


            
if(-1 == pid)
            
{
                Console.WriteLine(
"Invalid processID!");
                
return;
            }


            
string output = "";
            Process proc 
= null;
            
try
            
{
                proc 
= Process.GetProcessById(pid);
            }

            
catch
            
{

            }

            
if(null == proc)
            
{
                Console.WriteLine(
"Invalid processID!");
                
return;
            }


            Console.WriteLine(
"PID:"+proc.Id.ToString());
            
            Console.WriteLine(
"-".PadRight(50,'-'));
//            Console.WriteLine("Main Module");
//            Console.WriteLine("-".PadRight(50,'-'));
            Console.WriteLine("Module Name".PadRight(50)+"Memory Size");
            WriteModuleInformation(proc.MainModule);

            Console.WriteLine(
"-".PadRight(50,'-'));
//            Console.WriteLine("All Modules");
//            Console.WriteLine("-".PadRight(50,'-'));
            Console.WriteLine("Module Name".PadRight(50)+"Memory Size");
            
foreach(ProcessModule pm in proc.Modules)WriteModuleInformation(pm);
            
            Console.WriteLine(
"-".PadRight(50,'-'));
            Console.WriteLine(
"Thread List:");
            Console.WriteLine(
"-".PadRight(50,'-'));
            Console.WriteLine(
"ID".PadRight(6)+"Thread State".PadRight(20)+"Total Processor Time");
            Console.WriteLine(
"-".PadRight(50,'-'));
            
foreach(ProcessThread pt in proc.Threads)
            
{
                output 
= String.Format("{0}{1}{2}",pt.Id.ToString().PadRight(6),pt.ThreadState.ToString().PadRight(20),pt.TotalProcessorTime);
                Console.WriteLine(output);
            }

        }


        
private static void WriteModuleInformation(ProcessModule pm)
        
{
            Console.WriteLine(String.Format(
"{0}{1}",pm.ModuleName.PadRight(50),(pm.ModuleMemorySize/1024).ToString().PadLeft(10)+"K"));
        }

    }

}

posted @ 2004-10-12 16:27  鞠强  阅读(1541)  评论(4编辑  收藏  举报

hello

world