用WMI可以实现实现远程启动或者关掉一个进程的功能

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Diagnostics;
    using System.Data.SqlClient;
    using System.Security;
    using System.Security.Permissions;
    namespace Z3
    {
    public class Processes
    { public static DataSet ps( string machineName )
    {
    Process[] psList = Process.GetProcesses(machineName); DataSet ds = new DataSet("processes");

    ds.Tables.Add("ps");
    DataColumn[] cols = {new DataColumn("PID", typeof(string)), 
     new DataColumn("Name", typeof(string)),
     new DataColumn("Priority", typeof(string)),
     new DataColumn("vmSize", typeof(string)),
     new DataColumn("workingSetSize", typeof(string)),
     new DataColumn("threadCount", typeof(string)),
     new DataColumn("File", typeof(string)),
     new DataColumn("handleCount", typeof(string))}; ds.Tables["ps"].Columns.AddRange(cols);

    //table for process's modules
    ds.Tables.Add("modules"); DataColumn[] modcols = {new DataColumn("PID", typeof(string)), 
    new DataColumn("CompanyName", typeof(string)),
    new DataColumn("ModuleName", typeof(string)),
    new DataColumn("FileName", typeof(string)),
    new DataColumn("FileVersion", typeof(string)),
    new DataColumn("IsPatched", typeof(string))}; ds.Tables["modules"].Columns.AddRange(modcols); //hold the values
    object[] vals = new object[8];
    object[] modvals = new object[6];
    // Create a security permission object, needed for some reason on some machines
    SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.AllFlags);
    perm.Assert(); int psCount = psList.Length;
    int modCount;

    for( int i = 0; i < psCount; i++ )
    {
    try
    {
    // pid
    vals[0] = psList[i].Id;
    }
    catch( Exception )
    {
    vals[0] = "n/a";
    } try
    {
    // ProcessName
    vals[1] = psList[i].ProcessName;
    }
    catch( Exception )
    {
    vals[1] = "n/a";
    } try
    {
    // Priority
    vals[2] = psList[i].PriorityClass;
    }
    catch( Exception )
    {
    vals[2] = "n/a";
    } try
    {
    // VirtualMemorySize
    vals[3] = psList[i].VirtualMemorySize;
    }
    catch( Exception )
    {
    vals[3] = "n/a";
    } try
    {
    // WorkingSet
    vals[4] = psList[i].WorkingSet;
    }
    catch( Exception )
    {
    vals[4] = "n/a";
    } try
    {
    // ThreadCount
    vals[5] = psList[i].Threads.Count;
    }
    catch( Exception )
    {
    vals[5] = "n/a";
    } try
    {
    // HandleCount
    vals[7] = psList[i].HandleCount;
    }
    catch( Exception )
    {
    vals[7] = "n/a";
    }
    try
    {
    // FileName (full path)
    vals[6] = psList[i].MainModule.FileName;
    }
    catch( Exception )
    {
    vals[6] = "n/a";
    } //insert a new row
    ds.Tables["ps"].Rows.Add(vals);
    try
    {
    //iterate thru modules for this process
    modCount = psList[i].Modules.Count;
    for( int z = 0; z < modCount; z++ )
    {
    modvals[0] = vals[0]; try
    {
    modvals[1] = psList[i].Modules[z].FileVersionInfo.CompanyName;
    }
    catch( Exception )
    {
    modvals[1] = "unknown";
    }
    try
    {
    modvals[2] = psList[i].Modules[z].ModuleName;
    }
    catch( Exception )
    {
    modvals[2] = "unknown";
    }
    try
    {
    modvals[3] = psList[i].Modules[z].FileName;
    }
    catch( Exception )
    {
    modvals[3] = "unknown";
    }
    try
    {
    modvals[4] = psList[i].Modules[z].FileVersionInfo.FileVersion;
    }
    catch( Exception )
    {
    modvals[4] = "unknown";
    }
    try
    {
    modvals[5] = psList[i].Modules[z].FileVersionInfo.IsPatched;
    }
    catch( Exception )
    {
    modvals[5] = "unknown";
    } //insert a new row
    ds.Tables["modules"].Rows.Add(modvals);
    }
    }
    catch( Exception )
    {
    //nothing, this is for System and Idle processes, they don't have any modules
    }
    } // Create a relationship between tables
    ds.Relations.Add("Process_Modules", ds.Tables["ps"].Columns["PID"], ds.Tables["modules"].Columns["PID"]); return ds;
    } // IDispose
    public void Dispose()
    {
    // Not holding any external resources.
    } }
    }
      

  2.   

    使用WMI
    System.Management命名空间下的类。