プロセスの使用リソースとかの確認。
死ぬほどめんどい。なんで WMI はスクリプトなんだ。インテリセンスきかねーじゃねーか。
というわけでいろいろ手抜き。してもめんどい。
/// <summary> /// ProcessResourceInfo クラス。 /// </summary> public class ProcessResourceInfo : IDisposable { private readonly int _processId; private ManagementBaseObject _processItem; private ProcessResourceInfo _snapProcessItemInfo; public string Caption { get { return (string)_processItem["Caption"]; } } public string CommandLine { get { return (string)_processItem["CommandLine"]; } } public string CreationClassName { get { return (string)_processItem["CommandLine"]; } } public DateTime CreationDate { get { return ManagementDateTimeConverter.ToDateTime( (string)_processItem["CreationDate"] ); } } public string CsCreationClassName { get { return (string)_processItem["CSCreationClassName"]; } } public string CsName { get { return (string)_processItem["CSName"]; } } public string Description { get { return (string)_processItem["Description"]; } } public string ExecutablePath { get { return (string)_processItem["ExecutablePath"]; } } public UInt16? ExecutionState { get { return (UInt16?)_processItem["ExecutionState"]; } } public string Handle { get { return (string)_processItem["Handle"]; } } public UInt32 HandleCount { get { return (UInt32)_processItem["HandleCount"]; } } public DateTime? InstallDate { get { string value = (string)_processItem["InstallDate"]; return value == null ? null : (DateTime?)ManagementDateTimeConverter.ToDateTime( value ); } } public UInt64 KernelModeTime { get { return (UInt64)_processItem["KernelModeTime"]; } } public UInt32 MaximumWorkingSetSize { get { return (UInt32)_processItem["MaximumWorkingSetSize"]; } } public UInt32 MinimumWorkingSetSize { get { return (UInt32)_processItem["MinimumWorkingSetSize"]; } } public string Name { get { return (string)_processItem["Name"]; } } public string OsCreationClassName { get { return (string)_processItem["OSCreationClassName"]; } } public string OsName { get { return (string)_processItem["OSName"]; } } public UInt64 OtherOperationCount { get { return (UInt64)_processItem["OtherOperationCount"]; } } public UInt64 OtherTransferCount { get { return (UInt64)_processItem["OtherTransferCount"]; } } public UInt32 PageFaults { get { return (UInt32)_processItem["PageFaults"]; } } public UInt32 PageFileUsage { get { return (UInt32)_processItem["PageFileUsage"]; } } public UInt32 ParentProcessId { get { return (UInt32)_processItem["ParentProcessId"]; } } public UInt32 PeakPageFileUsage { get { return (UInt32)_processItem["PeakPageFileUsage"]; } } public UInt64 PeakVirtualSize { get { return (UInt64)_processItem["PeakVirtualSize"]; } } public UInt32 PeakWorkingSetSize { get { return (UInt32)_processItem["PeakWorkingSetSize"]; } } public UInt32 Priority { get { return (UInt32)_processItem["Priority"]; } } public UInt64 PrivatePageCount { get { return (UInt64)_processItem["PrivatePageCount"]; } } public UInt32 ProcessId { get { return (UInt32)_processItem["ProcessId"]; } } public UInt32 QuotaNonPagedPoolUsage { get { return (UInt32)_processItem["QuotaNonPagedPoolUsage"]; } } public UInt32 QuotaPagedPoolUsage { get { return (UInt32)_processItem["QuotaPagedPoolUsage"]; } } public UInt32 QuotaPeakNonPagedPoolUsage { get { return (UInt32)_processItem["QuotaPeakNonPagedPoolUsage"]; } } public UInt32 QuotaPeakPagedPoolUsage { get { return (UInt32)_processItem["QuotaPeakPagedPoolUsage"]; } } public UInt64 ReadOperationCount { get { return (UInt64)_processItem["ReadOperationCount"]; } } public UInt64 ReadTransferCount { get { return (UInt64)_processItem["ReadTransferCount"]; } } public UInt32 SessionId { get { return (UInt32)_processItem["SessionId"]; } } public string Status { get { return (string)_processItem["Status"]; } } public DateTime? TerminationDate { get { string value = (string) _processItem["TerminationDate"]; return value == null ? null : (DateTime?) ManagementDateTimeConverter.ToDateTime( value ); } } public UInt32 ThreadCount { get { return (UInt32)_processItem["ThreadCount"]; } } public UInt64 UserModeTime { get { return (UInt64)_processItem["UserModeTime"]; } } public UInt64 VirtualSize { get { return (UInt64)_processItem["VirtualSize"]; } } public string WindowsVersion { get { return (string)_processItem["WindowsVersion"]; } } public UInt64 WorkingSetSize { get { return (UInt64)_processItem["WorkingSetSize"]; } } public UInt64 WriteOperationCount { get { return (UInt64)_processItem["WriteOperationCount"]; } } public UInt64 WriteTransferCount { get { return (UInt64)_processItem["WriteTransferCount"]; } } public ProcessDifferenceValue DifferenceValue() { if ( _snapProcessItemInfo == null ) { Snapshot(); } return new ProcessDifferenceValue( _snapProcessItemInfo, this ); } public ProcessResourceInfo(int processId) { _processId = processId; _processItem = GetProcessItem( processId ); return; } private static ManagementBaseObject GetProcessItem( int processId ) { ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher( "SELECT * FROM Win32_Process Where ProcessId = '" + processId + "'" ); ManagementObjectCollection collection = searchProcedure.Get(); ManagementObjectCollection.ManagementObjectEnumerator enumerator = collection.GetEnumerator(); if (!enumerator.MoveNext()) { throw new ArgumentException( "指定されたプロセスの情報が取得できませんでした。", "processId" ); } return enumerator.Current; } public void Snapshot( bool doCollect ) { if (doCollect) { GC.Collect(); } _snapProcessItemInfo = new ProcessResourceInfo( _processId ); } public void Snapshot() { Snapshot( false ); } public void Reset() { Dispose(); _snapProcessItemInfo = null; _processItem = GetProcessItem( _processId ); } #region Dispose pattern /// <summary> /// リソースが解放されているかどうかを示します。 /// </summary> bool _disposed; /// <summary> /// ProcessResourceInfo で使用されるすべてのリソースを解放します。 /// </summary> public void Dispose() { GC.SuppressFinalize( this ); Dispose( true ); } /// <summary> /// ProcessResourceInfo によって使用されているアンマネージ リソースを解放し、オプションでマネージ リソースも解放します。 /// </summary> /// <param name="disposing">マネージ リソースとアンマネージ リソースの両方を解放する場合は true。アンマネージ リソースだけを解放する場合は false。</param> protected virtual void Dispose( bool disposing ) { if (_disposed) { return; } _disposed = true; if (disposing) { if (_processItem != null) { _processItem.Dispose(); } if (_snapProcessItemInfo != null) { _snapProcessItemInfo.Dispose(); } } } ~ProcessResourceInfo() { Dispose( false ); } #endregion public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat( "Caption {0}", Caption ).AppendLine(); sb.AppendFormat( "CommandLine {0}", CommandLine ).AppendLine(); sb.AppendFormat( "CreationClassName {0}", CreationClassName ).AppendLine(); sb.AppendFormat( "CreationDate {0}", CreationDate ).AppendLine(); sb.AppendFormat( "CsCreationClassName {0}", CsCreationClassName ).AppendLine(); sb.AppendFormat( "CsName {0}", CsName ).AppendLine(); sb.AppendFormat( "Description {0}", Description ).AppendLine(); sb.AppendFormat( "ExecutablePath {0}", ExecutablePath ).AppendLine(); sb.AppendFormat( "ExecutionState {0}", ExecutionState == null ? "" : ExecutionState.ToString() ).AppendLine(); sb.AppendFormat( "Handle {0}", Handle ).AppendLine(); sb.AppendFormat( "HandleCount {0}", HandleCount ).AppendLine(); sb.AppendFormat( "InstallDate {0}", InstallDate == null ? "" : InstallDate.ToString() ).AppendLine(); sb.AppendFormat( "KernelModeTime {0}", KernelModeTime ).AppendLine(); sb.AppendFormat( "MaximumWorkingSetSize {0}", MaximumWorkingSetSize ).AppendLine(); sb.AppendFormat( "MinimumWorkingSetSize {0}", MinimumWorkingSetSize ).AppendLine(); sb.AppendFormat( "Name {0}", Name ).AppendLine(); sb.AppendFormat( "OsCreationClassName {0}", OsCreationClassName ).AppendLine(); sb.AppendFormat( "OsName {0}", OsName ).AppendLine(); sb.AppendFormat( "OtherOperationCount {0}", OtherOperationCount ).AppendLine(); sb.AppendFormat( "OtherTransferCount {0}", OtherTransferCount ).AppendLine(); sb.AppendFormat( "PageFaults {0}", PageFaults ).AppendLine(); sb.AppendFormat( "PageFileUsage {0}", PageFileUsage ).AppendLine(); sb.AppendFormat( "ParentProcessId {0}", ParentProcessId ).AppendLine(); sb.AppendFormat( "PeakPageFileUsage {0}", PeakPageFileUsage ).AppendLine(); sb.AppendFormat( "PeakVirtualSize {0}", PeakVirtualSize ).AppendLine(); sb.AppendFormat( "PeakWorkingSetSize {0}", PeakWorkingSetSize ).AppendLine(); sb.AppendFormat( "Priority {0}", Priority ).AppendLine(); sb.AppendFormat( "PrivatePageCount {0}", PrivatePageCount ).AppendLine(); sb.AppendFormat( "ProcessId {0}", ProcessId ).AppendLine(); sb.AppendFormat( "QuotaNonPagedPoolUsage {0}", QuotaNonPagedPoolUsage ).AppendLine(); sb.AppendFormat( "QuotaPagedPoolUsage {0}", QuotaPagedPoolUsage ).AppendLine(); sb.AppendFormat( "QuotaPeakNonPagedPoolUsage {0}", QuotaPeakNonPagedPoolUsage ).AppendLine(); sb.AppendFormat( "QuotaPeakPagedPoolUsage {0}", QuotaPeakPagedPoolUsage ).AppendLine(); sb.AppendFormat( "ReadOperationCount {0}", ReadOperationCount ).AppendLine(); sb.AppendFormat( "ReadTransferCount {0}", ReadTransferCount ).AppendLine(); sb.AppendFormat( "SessionId {0}", SessionId ).AppendLine(); sb.AppendFormat( "Status {0}", Status ).AppendLine(); sb.AppendFormat( "TerminationDate {0}", TerminationDate == null ? "" : TerminationDate.ToString() ).AppendLine(); sb.AppendFormat( "ThreadCount {0}", ThreadCount ).AppendLine(); sb.AppendFormat( "UserModeTime {0}", UserModeTime ).AppendLine(); sb.AppendFormat( "VirtualSize {0}", VirtualSize ).AppendLine(); sb.AppendFormat( "WorkingSetSize {0}", WorkingSetSize ).AppendLine(); sb.AppendFormat( "WriteOperationCount {0}", WriteOperationCount ).AppendLine(); sb.AppendFormat( "WriteTransferCount {0}", WriteTransferCount ).AppendLine(); return sb.ToString(); } }
ProcessDifferenceValue 差分のあれこれ。
/// <summary> /// ProcessDifferenceValue クラス。 /// </summary> public struct ProcessDifferenceValue { public Int64 HandleCount { get; private set; } public decimal OtherOperationCount { get; private set; } public decimal OtherTransferCount { get; private set; } public Int64 PageFaults { get; private set; } public Int64 PageFileUsage { get; private set; } public Int64 PeakPageFileUsage { get; private set; } public decimal PeakVirtualSize { get; private set; } public Int64 PeakWorkingSetSize { get; private set; } public decimal PrivatePageCount { get; private set; } public Int64 QuotaNonPagedPoolUsage { get; private set; } public Int64 QuotaPagedPoolUsage { get; private set; } public Int64 QuotaPeakNonPagedPoolUsage { get; private set; } public Int64 QuotaPeakPagedPoolUsage { get; private set; } public decimal ReadOperationCount { get; private set; } public decimal ReadTransferCount { get; private set; } public Int64 ThreadCount { get; private set; } public decimal UserModeTime { get; private set; } public decimal VirtualSize { get; private set; } public decimal WorkingSetSize { get; private set; } public decimal WriteOperationCount { get; private set; } public decimal WriteTransferCount { get; private set; } public ProcessDifferenceValue( ProcessResourceInfo destination, ProcessResourceInfo source ) : this() { HandleCount = (long)destination.HandleCount - source.HandleCount; OtherOperationCount = (decimal)destination.OtherOperationCount - source.OtherOperationCount; OtherTransferCount = (decimal)destination.OtherTransferCount - source.OtherTransferCount; PageFaults = (long)destination.PageFaults - source.PageFaults; PageFileUsage = (long)destination.PageFileUsage - source.PageFileUsage; PeakPageFileUsage = (long)destination.PeakPageFileUsage - source.PeakPageFileUsage; PeakVirtualSize = (decimal)destination.PeakVirtualSize - source.PeakVirtualSize; PeakWorkingSetSize = (long)destination.PeakWorkingSetSize - source.PeakWorkingSetSize; PrivatePageCount = (decimal)destination.PrivatePageCount - source.PrivatePageCount; QuotaNonPagedPoolUsage = (long)destination.QuotaNonPagedPoolUsage - source.QuotaNonPagedPoolUsage; QuotaPagedPoolUsage = (long)destination.QuotaPagedPoolUsage - source.QuotaPagedPoolUsage; QuotaPeakNonPagedPoolUsage = (long)destination.QuotaPeakNonPagedPoolUsage - source.QuotaPeakNonPagedPoolUsage; QuotaPeakPagedPoolUsage = (long)destination.QuotaPeakPagedPoolUsage - source.QuotaPeakPagedPoolUsage; ReadOperationCount = (decimal)destination.ReadOperationCount - source.ReadOperationCount; ReadTransferCount = (decimal)destination.ReadTransferCount - source.ReadTransferCount; ThreadCount = (long)destination.ThreadCount - source.ThreadCount; UserModeTime = (decimal)destination.UserModeTime - source.UserModeTime; VirtualSize = (decimal)destination.VirtualSize - source.VirtualSize; WorkingSetSize = (decimal)destination.WorkingSetSize - source.WorkingSetSize; WriteOperationCount = (decimal)destination.WriteOperationCount - source.WriteOperationCount; WriteTransferCount = (decimal)destination.WriteTransferCount - source.WriteTransferCount; } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat( "HandleCount {0}", HandleCount ).AppendLine(); sb.AppendFormat( "OtherOperationCount {0}", OtherOperationCount ).AppendLine(); sb.AppendFormat( "OtherTransferCount {0}", OtherTransferCount ).AppendLine(); sb.AppendFormat( "PageFaults {0}", PageFaults ).AppendLine(); sb.AppendFormat( "PageFileUsage {0}", PageFileUsage ).AppendLine(); sb.AppendFormat( "PeakPageFileUsage {0}", PeakPageFileUsage ).AppendLine(); sb.AppendFormat( "PeakVirtualSize {0}", PeakVirtualSize ).AppendLine(); sb.AppendFormat( "PeakWorkingSetSize {0}", PeakWorkingSetSize ).AppendLine(); sb.AppendFormat( "PrivatePageCount {0}", PrivatePageCount ).AppendLine(); sb.AppendFormat( "QuotaNonPagedPoolUsage {0}", QuotaNonPagedPoolUsage ).AppendLine(); sb.AppendFormat( "QuotaPagedPoolUsage {0}", QuotaPagedPoolUsage ).AppendLine(); sb.AppendFormat( "QuotaPeakNonPagedPoolUsage {0}", QuotaPeakNonPagedPoolUsage ).AppendLine(); sb.AppendFormat( "QuotaPeakPagedPoolUsage {0}", QuotaPeakPagedPoolUsage ).AppendLine(); sb.AppendFormat( "ReadOperationCount {0}", ReadOperationCount ).AppendLine(); sb.AppendFormat( "ReadTransferCount {0}", ReadTransferCount ).AppendLine(); sb.AppendFormat( "ThreadCount {0}", ThreadCount ).AppendLine(); sb.AppendFormat( "UserModeTime {0}", UserModeTime ).AppendLine(); sb.AppendFormat( "VirtualSize {0}", VirtualSize ).AppendLine(); sb.AppendFormat( "WorkingSetSize {0}", WorkingSetSize ).AppendLine(); sb.AppendFormat( "WriteOperationCount {0}", WriteOperationCount ).AppendLine(); sb.AppendFormat( "WriteTransferCount {0}", WriteTransferCount ).AppendLine(); return sb.ToString(); } }
幸せになる方法。
ProcessResourceInfo info = new ProcessResourceInfo( Process.GetCurrentProcess().Id ); // ここで、ごにょごにょ処理する。 Console.WriteLine( info.DifferenceValue() ); info.Dispose();