Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 131.14 KB | None | 0 0
  1. // Decompiled with JetBrains decompiler
  2. // Type: System.Diagnostics.Process
  3. // Assembly: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  4. // MVID: 988FA076-10C9-4365-AE6D-295A6AA379FE
  5. // Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll
  6.  
  7. using Microsoft.Win32;
  8. using Microsoft.Win32.SafeHandles;
  9. using System.Collections;
  10. using System.ComponentModel;
  11. using System.ComponentModel.Design;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Runtime.CompilerServices;
  15. using System.Runtime.InteropServices;
  16. using System.Security;
  17. using System.Security.Permissions;
  18. using System.Text;
  19. using System.Threading;
  20.  
  21. namespace System.Diagnostics
  22. {
  23.   /// <summary>Provides access to local and remote processes and enables you to start and stop local system processes.To browse the .NET Framework source code for this type, see the Reference Source.</summary>
  24.   [MonitoringDescription("ProcessDesc")]
  25.   [DefaultEvent("Exited")]
  26.   [DefaultProperty("StartInfo")]
  27.   [Designer("System.Diagnostics.Design.ProcessDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  28.   [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  29.   [HostProtection(SecurityAction.LinkDemand, ExternalProcessMgmt = true, SelfAffectingProcessMgmt = true, SharedState = true, Synchronization = true)]
  30.   [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
  31.   public class Process : Component
  32.   {
  33.     private static object s_CreateProcessLock = new object();
  34.     private static SafeFileHandle InvalidPipeHandle = new SafeFileHandle(IntPtr.Zero, false);
  35.     internal static TraceSwitch processTracing = (TraceSwitch) null;
  36.     private bool haveProcessId;
  37.     private int processId;
  38.     private bool haveProcessHandle;
  39.     private SafeProcessHandle m_processHandle;
  40.     private bool isRemoteMachine;
  41.     private string machineName;
  42.     private ProcessInfo processInfo;
  43.     private int m_processAccess;
  44.     private ProcessThreadCollection threads;
  45.     private ProcessModuleCollection modules;
  46.     private bool haveMainWindow;
  47.     private IntPtr mainWindowHandle;
  48.     private string mainWindowTitle;
  49.     private bool haveWorkingSetLimits;
  50.     private IntPtr minWorkingSet;
  51.     private IntPtr maxWorkingSet;
  52.     private bool haveProcessorAffinity;
  53.     private IntPtr processorAffinity;
  54.     private bool havePriorityClass;
  55.     private ProcessPriorityClass priorityClass;
  56.     private ProcessStartInfo startInfo;
  57.     private bool watchForExit;
  58.     private bool watchingForExit;
  59.     private EventHandler onExited;
  60.     private bool exited;
  61.     private int exitCode;
  62.     private bool signaled;
  63.     private DateTime exitTime;
  64.     private bool haveExitTime;
  65.     private bool responding;
  66.     private bool haveResponding;
  67.     private bool priorityBoostEnabled;
  68.     private bool havePriorityBoostEnabled;
  69.     private bool raisedOnExited;
  70.     private RegisteredWaitHandle registeredWaitHandle;
  71.     private WaitHandle waitHandle;
  72.     private ISynchronizeInvoke synchronizingObject;
  73.     private StreamReader standardOutput;
  74.     private StreamWriter standardInput;
  75.     private StreamReader standardError;
  76.     private OperatingSystem operatingSystem;
  77.     private bool disposed;
  78.     private Process.StreamReadMode outputStreamReadMode;
  79.     private Process.StreamReadMode errorStreamReadMode;
  80.     internal AsyncStreamReader output;
  81.     internal AsyncStreamReader error;
  82.     internal bool pendingOutputRead;
  83.     internal bool pendingErrorRead;
  84.  
  85.     /// <summary>Occurs each time an application writes a line to its redirected <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream. </summary>
  86.     [Browsable(true)]
  87.     [MonitoringDescription("ProcessAssociated")]
  88.     public event DataReceivedEventHandler OutputDataReceived;
  89.  
  90.     /// <summary>Occurs when an application writes to its redirected <see cref="P:System.Diagnostics.Process.StandardError" /> stream.</summary>
  91.     [Browsable(true)]
  92.     [MonitoringDescription("ProcessAssociated")]
  93.     public event DataReceivedEventHandler ErrorDataReceived;
  94.  
  95.     /// <summary>Initializes a new instance of the <see cref="T:System.Diagnostics.Process" /> class.</summary>
  96.     public Process()
  97.     {
  98.       this.machineName = ".";
  99.       this.outputStreamReadMode = Process.StreamReadMode.undefined;
  100.       this.errorStreamReadMode = Process.StreamReadMode.undefined;
  101.       this.m_processAccess = 2035711;
  102.     }
  103.  
  104.     private Process(
  105.       string machineName,
  106.       bool isRemoteMachine,
  107.       int processId,
  108.       ProcessInfo processInfo)
  109.     {
  110.       this.processInfo = processInfo;
  111.       this.machineName = machineName;
  112.       this.isRemoteMachine = isRemoteMachine;
  113.       this.processId = processId;
  114.       this.haveProcessId = true;
  115.       this.outputStreamReadMode = Process.StreamReadMode.undefined;
  116.       this.errorStreamReadMode = Process.StreamReadMode.undefined;
  117.       this.m_processAccess = 2035711;
  118.     }
  119.  
  120.     [Browsable(false)]
  121.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  122.     [MonitoringDescription("ProcessAssociated")]
  123.     private bool Associated
  124.     {
  125.       get
  126.       {
  127.         if (!this.haveProcessId)
  128.           return this.haveProcessHandle;
  129.         return true;
  130.       }
  131.     }
  132.  
  133.     /// <summary>Gets the base priority of the associated process.</summary>
  134.     /// <returns>The base priority, which is computed from the <see cref="P:System.Diagnostics.Process.PriorityClass" /> of the associated process.</returns>
  135.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set the <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> property to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  136.     /// <exception cref="T:System.InvalidOperationException">The process has exited.-or- The process has not started, so there is no process ID. </exception>
  137.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  138.     [MonitoringDescription("ProcessBasePriority")]
  139.     public int BasePriority
  140.     {
  141.       get
  142.       {
  143.         this.EnsureState(Process.State.HaveProcessInfo);
  144.         return this.processInfo.basePriority;
  145.       }
  146.     }
  147.  
  148.     /// <summary>Gets the value that the associated process specified when it terminated.</summary>
  149.     /// <returns>The code that the associated process specified when it terminated.</returns>
  150.     /// <exception cref="T:System.InvalidOperationException">The process has not exited.-or- The process <see cref="P:System.Diagnostics.Process.Handle" /> is not valid. </exception>
  151.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.ExitCode" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  152.     [Browsable(false)]
  153.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  154.     [MonitoringDescription("ProcessExitCode")]
  155.     public int ExitCode
  156.     {
  157.       get
  158.       {
  159.         this.EnsureState(Process.State.Exited);
  160.         return this.exitCode;
  161.       }
  162.     }
  163.  
  164.     /// <summary>Gets a value indicating whether the associated process has been terminated.</summary>
  165.     /// <returns>
  166.     /// <see langword="true" /> if the operating system process referenced by the <see cref="T:System.Diagnostics.Process" /> component has terminated; otherwise, <see langword="false" />.</returns>
  167.     /// <exception cref="T:System.InvalidOperationException">There is no process associated with the object. </exception>
  168.     /// <exception cref="T:System.ComponentModel.Win32Exception">The exit code for the process could not be retrieved. </exception>
  169.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.HasExited" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  170.     [Browsable(false)]
  171.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  172.     [MonitoringDescription("ProcessTerminated")]
  173.     public bool HasExited
  174.     {
  175.       get
  176.       {
  177.         if (!this.exited)
  178.         {
  179.           this.EnsureState(Process.State.Associated);
  180.           SafeProcessHandle safeProcessHandle = (SafeProcessHandle) null;
  181.           try
  182.           {
  183.             safeProcessHandle = this.GetProcessHandle(1049600, false);
  184.             if (safeProcessHandle.IsInvalid)
  185.             {
  186.               this.exited = true;
  187.             }
  188.             else
  189.             {
  190.               int exitCode;
  191.               if (Microsoft.Win32.NativeMethods.GetExitCodeProcess(safeProcessHandle, out exitCode) && exitCode != 259)
  192.               {
  193.                 this.exited = true;
  194.                 this.exitCode = exitCode;
  195.               }
  196.               else
  197.               {
  198.                 if (!this.signaled)
  199.                 {
  200.                   ProcessWaitHandle processWaitHandle = (ProcessWaitHandle) null;
  201.                   try
  202.                   {
  203.                     processWaitHandle = new ProcessWaitHandle(safeProcessHandle);
  204.                     this.signaled = processWaitHandle.WaitOne(0, false);
  205.                   }
  206.                   finally
  207.                   {
  208.                     processWaitHandle?.Close();
  209.                   }
  210.                 }
  211.                 if (this.signaled)
  212.                 {
  213.                   if (!Microsoft.Win32.NativeMethods.GetExitCodeProcess(safeProcessHandle, out exitCode))
  214.                     throw new Win32Exception();
  215.                   this.exited = true;
  216.                   this.exitCode = exitCode;
  217.                 }
  218.               }
  219.             }
  220.           }
  221.           finally
  222.           {
  223.             this.ReleaseProcessHandle(safeProcessHandle);
  224.           }
  225.           if (this.exited)
  226.             this.RaiseOnExited();
  227.         }
  228.         return this.exited;
  229.       }
  230.     }
  231.  
  232.     private ProcessThreadTimes GetProcessTimes()
  233.     {
  234.       ProcessThreadTimes processThreadTimes = new ProcessThreadTimes();
  235.       SafeProcessHandle handle = (SafeProcessHandle) null;
  236.       try
  237.       {
  238.         int access = 1024;
  239.         if (EnvironmentHelpers.IsWindowsVistaOrAbove())
  240.           access = 4096;
  241.         handle = this.GetProcessHandle(access, false);
  242.         if (handle.IsInvalid)
  243.           throw new InvalidOperationException(SR.GetString("ProcessHasExited", (object) this.processId.ToString((IFormatProvider) CultureInfo.CurrentCulture)));
  244.         if (!Microsoft.Win32.NativeMethods.GetProcessTimes(handle, out processThreadTimes.create, out processThreadTimes.exit, out processThreadTimes.kernel, out processThreadTimes.user))
  245.           throw new Win32Exception();
  246.       }
  247.       finally
  248.       {
  249.         this.ReleaseProcessHandle(handle);
  250.       }
  251.       return processThreadTimes;
  252.     }
  253.  
  254.     /// <summary>Gets the time that the associated process exited.</summary>
  255.     /// <returns>A <see cref="T:System.DateTime" /> that indicates when the associated process was terminated.</returns>
  256.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  257.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.ExitTime" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  258.     [Browsable(false)]
  259.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  260.     [MonitoringDescription("ProcessExitTime")]
  261.     public DateTime ExitTime
  262.     {
  263.       get
  264.       {
  265.         if (!this.haveExitTime)
  266.         {
  267.           this.EnsureState(Process.State.IsNt | Process.State.Exited);
  268.           this.exitTime = this.GetProcessTimes().ExitTime;
  269.           this.haveExitTime = true;
  270.         }
  271.         return this.exitTime;
  272.       }
  273.     }
  274.  
  275.     /// <summary>Gets the native handle of the associated process.</summary>
  276.     /// <returns>The handle that the operating system assigned to the associated process when the process was started. The system uses this handle to keep track of process attributes.</returns>
  277.     /// <exception cref="T:System.InvalidOperationException">The process has not been started or has exited. The <see cref="P:System.Diagnostics.Process.Handle" /> property cannot be read because there is no process associated with this <see cref="T:System.Diagnostics.Process" /> instance.-or- The <see cref="T:System.Diagnostics.Process" /> instance has been attached to a running process but you do not have the necessary permissions to get a handle with full access rights. </exception>
  278.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.Handle" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  279.     [Browsable(false)]
  280.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  281.     [MonitoringDescription("ProcessHandle")]
  282.     public IntPtr Handle
  283.     {
  284.       get
  285.       {
  286.         this.EnsureState(Process.State.Associated);
  287.         return this.OpenProcessHandle(this.m_processAccess).DangerousGetHandle();
  288.       }
  289.     }
  290.  
  291.     /// <summary>Gets the native handle to this process.</summary>
  292.     /// <returns>The native handle to this process.</returns>
  293.     [Browsable(false)]
  294.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  295.     public SafeProcessHandle SafeHandle
  296.     {
  297.       get
  298.       {
  299.         this.EnsureState(Process.State.Associated);
  300.         return this.OpenProcessHandle(this.m_processAccess);
  301.       }
  302.     }
  303.  
  304.     /// <summary>Gets the number of handles opened by the process.</summary>
  305.     /// <returns>The number of operating system handles the process has opened.</returns>
  306.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set the <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> property to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  307.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  308.     [MonitoringDescription("ProcessHandleCount")]
  309.     public int HandleCount
  310.     {
  311.       get
  312.       {
  313.         this.EnsureState(Process.State.HaveProcessInfo);
  314.         return this.processInfo.handleCount;
  315.       }
  316.     }
  317.  
  318.     /// <summary>Gets the unique identifier for the associated process.</summary>
  319.     /// <returns>The system-generated unique identifier of the process that is referenced by this <see cref="T:System.Diagnostics.Process" /> instance.</returns>
  320.     /// <exception cref="T:System.InvalidOperationException">The process's <see cref="P:System.Diagnostics.Process.Id" /> property has not been set.-or- There is no process associated with this <see cref="T:System.Diagnostics.Process" /> object. </exception>
  321.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set the <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> property to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  322.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  323.     [MonitoringDescription("ProcessId")]
  324.     public int Id
  325.     {
  326.       get
  327.       {
  328.         this.EnsureState(Process.State.HaveId);
  329.         return this.processId;
  330.       }
  331.     }
  332.  
  333.     /// <summary>Gets the name of the computer the associated process is running on.</summary>
  334.     /// <returns>The name of the computer that the associated process is running on.</returns>
  335.     /// <exception cref="T:System.InvalidOperationException">There is no process associated with this <see cref="T:System.Diagnostics.Process" /> object. </exception>
  336.     [Browsable(false)]
  337.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  338.     [MonitoringDescription("ProcessMachineName")]
  339.     public string MachineName
  340.     {
  341.       get
  342.       {
  343.         this.EnsureState(Process.State.Associated);
  344.         return this.machineName;
  345.       }
  346.     }
  347.  
  348.     /// <summary>Gets the window handle of the main window of the associated process.</summary>
  349.     /// <returns>The system-generated window handle of the main window of the associated process.</returns>
  350.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.Process.MainWindowHandle" /> is not defined because the process has exited. </exception>
  351.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.MainWindowHandle" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  352.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  353.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  354.     [MonitoringDescription("ProcessMainWindowHandle")]
  355.     public IntPtr MainWindowHandle
  356.     {
  357.       get
  358.       {
  359.         if (!this.haveMainWindow)
  360.         {
  361.           this.EnsureState(Process.State.HaveId | Process.State.IsLocal);
  362.           this.mainWindowHandle = ProcessManager.GetMainWindowHandle(this.processId);
  363.           if (this.mainWindowHandle != (IntPtr) 0)
  364.             this.haveMainWindow = true;
  365.           else
  366.             this.EnsureState(Process.State.HaveProcessInfo);
  367.         }
  368.         return this.mainWindowHandle;
  369.       }
  370.     }
  371.  
  372.     /// <summary>Gets the caption of the main window of the process.</summary>
  373.     /// <returns>The main window title of the process.</returns>
  374.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.Process.MainWindowTitle" /> property is not defined because the process has exited. </exception>
  375.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.MainWindowTitle" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  376.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  377.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  378.     [MonitoringDescription("ProcessMainWindowTitle")]
  379.     public string MainWindowTitle
  380.     {
  381.       get
  382.       {
  383.         if (this.mainWindowTitle == null)
  384.         {
  385.           IntPtr mainWindowHandle = this.MainWindowHandle;
  386.           if (mainWindowHandle == (IntPtr) 0)
  387.           {
  388.             this.mainWindowTitle = string.Empty;
  389.           }
  390.           else
  391.           {
  392.             StringBuilder lpString = new StringBuilder(Microsoft.Win32.NativeMethods.GetWindowTextLength(new HandleRef((object) this, mainWindowHandle)) * 2);
  393.             Microsoft.Win32.NativeMethods.GetWindowText(new HandleRef((object) this, mainWindowHandle), lpString, lpString.Capacity);
  394.             this.mainWindowTitle = lpString.ToString();
  395.           }
  396.         }
  397.         return this.mainWindowTitle;
  398.       }
  399.     }
  400.  
  401.     /// <summary>Gets the main module for the associated process.</summary>
  402.     /// <returns>The <see cref="T:System.Diagnostics.ProcessModule" /> that was used to start the process.</returns>
  403.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.MainModule" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  404.     /// <exception cref="T:System.ComponentModel.Win32Exception">A 32-bit process is trying to access the modules of a 64-bit process.</exception>
  405.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  406.     /// <exception cref="T:System.InvalidOperationException">The process <see cref="P:System.Diagnostics.Process.Id" /> is not available.-or- The process has exited. </exception>
  407.     [Browsable(false)]
  408.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  409.     [MonitoringDescription("ProcessMainModule")]
  410.     public ProcessModule MainModule
  411.     {
  412.       get
  413.       {
  414.         if (this.OperatingSystem.Platform == PlatformID.Win32NT)
  415.         {
  416.           this.EnsureState(Process.State.HaveId | Process.State.IsLocal);
  417.           return new ProcessModule(NtProcessManager.GetFirstModuleInfo(this.processId));
  418.         }
  419.         ProcessModuleCollection modules = this.Modules;
  420.         this.EnsureState(Process.State.HaveProcessInfo);
  421.         foreach (ProcessModule processModule in (ReadOnlyCollectionBase) modules)
  422.         {
  423.           if (processModule.moduleInfo.Id == this.processInfo.mainModuleId)
  424.             return processModule;
  425.         }
  426.         return (ProcessModule) null;
  427.       }
  428.     }
  429.  
  430.     /// <summary>Gets or sets the maximum allowable working set size, in bytes, for the associated process.</summary>
  431.     /// <returns>The maximum working set size that is allowed in memory for the process, in bytes.</returns>
  432.     /// <exception cref="T:System.ArgumentException">The maximum working set size is invalid. It must be greater than or equal to the minimum working set size.</exception>
  433.     /// <exception cref="T:System.ComponentModel.Win32Exception">Working set information cannot be retrieved from the associated process resource.-or- The process identifier or process handle is zero because the process has not been started. </exception>
  434.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.MaxWorkingSet" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.</exception>
  435.     /// <exception cref="T:System.InvalidOperationException">The process <see cref="P:System.Diagnostics.Process.Id" /> is not available.-or- The process has exited. </exception>
  436.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  437.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  438.     [MonitoringDescription("ProcessMaxWorkingSet")]
  439.     public IntPtr MaxWorkingSet
  440.     {
  441.       get
  442.       {
  443.         this.EnsureWorkingSetLimits();
  444.         return this.maxWorkingSet;
  445.       }
  446.       set
  447.       {
  448.         this.SetWorkingSetLimits((object) null, (object) value);
  449.       }
  450.     }
  451.  
  452.     /// <summary>Gets or sets the minimum allowable working set size, in bytes, for the associated process.</summary>
  453.     /// <returns>The minimum working set size that is required in memory for the process, in bytes.</returns>
  454.     /// <exception cref="T:System.ArgumentException">The minimum working set size is invalid. It must be less than or equal to the maximum working set size.</exception>
  455.     /// <exception cref="T:System.ComponentModel.Win32Exception">Working set information cannot be retrieved from the associated process resource.-or- The process identifier or process handle is zero because the process has not been started. </exception>
  456.     /// <exception cref="T:System.NotSupportedException">You are trying to access the <see cref="P:System.Diagnostics.Process.MinWorkingSet" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  457.     /// <exception cref="T:System.InvalidOperationException">The process <see cref="P:System.Diagnostics.Process.Id" /> is not available.-or- The process has exited.</exception>
  458.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  459.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  460.     [MonitoringDescription("ProcessMinWorkingSet")]
  461.     public IntPtr MinWorkingSet
  462.     {
  463.       get
  464.       {
  465.         this.EnsureWorkingSetLimits();
  466.         return this.minWorkingSet;
  467.       }
  468.       set
  469.       {
  470.         this.SetWorkingSetLimits((object) value, (object) null);
  471.       }
  472.     }
  473.  
  474.     /// <summary>Gets the modules that have been loaded by the associated process.</summary>
  475.     /// <returns>An array of type <see cref="T:System.Diagnostics.ProcessModule" /> that represents the modules that have been loaded by the associated process.</returns>
  476.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.Modules" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  477.     /// <exception cref="T:System.InvalidOperationException">The process <see cref="P:System.Diagnostics.Process.Id" /> is not available.</exception>
  478.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  479.     /// <exception cref="T:System.ComponentModel.Win32Exception">You are attempting to access the <see cref="P:System.Diagnostics.Process.Modules" /> property for either the system process or the idle process. These processes do not have modules.</exception>
  480.     [Browsable(false)]
  481.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  482.     [MonitoringDescription("ProcessModules")]
  483.     public ProcessModuleCollection Modules
  484.     {
  485.       get
  486.       {
  487.         if (this.modules == null)
  488.         {
  489.           this.EnsureState(Process.State.HaveId | Process.State.IsLocal);
  490.           ModuleInfo[] moduleInfos = ProcessManager.GetModuleInfos(this.processId);
  491.           ProcessModule[] processModules = new ProcessModule[moduleInfos.Length];
  492.           for (int index = 0; index < moduleInfos.Length; ++index)
  493.             processModules[index] = new ProcessModule(moduleInfos[index]);
  494.           this.modules = new ProcessModuleCollection(processModules);
  495.         }
  496.         return this.modules;
  497.       }
  498.     }
  499.  
  500.     /// <summary>Gets the amount of nonpaged system memory, in bytes, allocated for the associated process.</summary>
  501.     /// <returns>The amount of memory, in bytes, the system has allocated for the associated process that cannot be written to the virtual memory paging file.</returns>
  502.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  503.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.NonpagedSystemMemorySize64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  504.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  505.     [MonitoringDescription("ProcessNonpagedSystemMemorySize")]
  506.     public int NonpagedSystemMemorySize
  507.     {
  508.       get
  509.       {
  510.         this.EnsureState(Process.State.HaveNtProcessInfo);
  511.         return (int) this.processInfo.poolNonpagedBytes;
  512.       }
  513.     }
  514.  
  515.     /// <summary>Gets the amount of nonpaged system memory, in bytes, allocated for the associated process.</summary>
  516.     /// <returns>The amount of system memory, in bytes, allocated for the associated process that cannot be written to the virtual memory paging file.</returns>
  517.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  518.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  519.     [MonitoringDescription("ProcessNonpagedSystemMemorySize")]
  520.     [ComVisible(false)]
  521.     public long NonpagedSystemMemorySize64
  522.     {
  523.       get
  524.       {
  525.         this.EnsureState(Process.State.HaveNtProcessInfo);
  526.         return this.processInfo.poolNonpagedBytes;
  527.       }
  528.     }
  529.  
  530.     /// <summary>Gets the amount of paged memory, in bytes, allocated for the associated process.</summary>
  531.     /// <returns>The amount of memory, in bytes, allocated by the associated process that can be written to the virtual memory paging file.</returns>
  532.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  533.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.PagedMemorySize64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  534.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  535.     [MonitoringDescription("ProcessPagedMemorySize")]
  536.     public int PagedMemorySize
  537.     {
  538.       get
  539.       {
  540.         this.EnsureState(Process.State.HaveNtProcessInfo);
  541.         return (int) this.processInfo.pageFileBytes;
  542.       }
  543.     }
  544.  
  545.     /// <summary>Gets the amount of paged memory, in bytes, allocated for the associated process.</summary>
  546.     /// <returns>The amount of memory, in bytes, allocated in the virtual memory paging file for the associated process.</returns>
  547.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  548.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  549.     [MonitoringDescription("ProcessPagedMemorySize")]
  550.     [ComVisible(false)]
  551.     public long PagedMemorySize64
  552.     {
  553.       get
  554.       {
  555.         this.EnsureState(Process.State.HaveNtProcessInfo);
  556.         return this.processInfo.pageFileBytes;
  557.       }
  558.     }
  559.  
  560.     /// <summary>Gets the amount of pageable system memory, in bytes, allocated for the associated process.</summary>
  561.     /// <returns>The amount of memory, in bytes, the system has allocated for the associated process that can be written to the virtual memory paging file.</returns>
  562.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  563.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.PagedSystemMemorySize64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  564.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  565.     [MonitoringDescription("ProcessPagedSystemMemorySize")]
  566.     public int PagedSystemMemorySize
  567.     {
  568.       get
  569.       {
  570.         this.EnsureState(Process.State.HaveNtProcessInfo);
  571.         return (int) this.processInfo.poolPagedBytes;
  572.       }
  573.     }
  574.  
  575.     /// <summary>Gets the amount of pageable system memory, in bytes, allocated for the associated process.</summary>
  576.     /// <returns>The amount of system memory, in bytes, allocated for the associated process that can be written to the virtual memory paging file.</returns>
  577.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  578.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  579.     [MonitoringDescription("ProcessPagedSystemMemorySize")]
  580.     [ComVisible(false)]
  581.     public long PagedSystemMemorySize64
  582.     {
  583.       get
  584.       {
  585.         this.EnsureState(Process.State.HaveNtProcessInfo);
  586.         return this.processInfo.poolPagedBytes;
  587.       }
  588.     }
  589.  
  590.     /// <summary>Gets the maximum amount of memory in the virtual memory paging file, in bytes, used by the associated process.</summary>
  591.     /// <returns>The maximum amount of memory, in bytes, allocated by the associated process that could be written to the virtual memory paging file.</returns>
  592.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  593.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.PeakPagedMemorySize64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  594.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  595.     [MonitoringDescription("ProcessPeakPagedMemorySize")]
  596.     public int PeakPagedMemorySize
  597.     {
  598.       get
  599.       {
  600.         this.EnsureState(Process.State.HaveNtProcessInfo);
  601.         return (int) this.processInfo.pageFileBytesPeak;
  602.       }
  603.     }
  604.  
  605.     /// <summary>Gets the maximum amount of memory in the virtual memory paging file, in bytes, used by the associated process.</summary>
  606.     /// <returns>The maximum amount of memory, in bytes, allocated in the virtual memory paging file for the associated process since it was started.</returns>
  607.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  608.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  609.     [MonitoringDescription("ProcessPeakPagedMemorySize")]
  610.     [ComVisible(false)]
  611.     public long PeakPagedMemorySize64
  612.     {
  613.       get
  614.       {
  615.         this.EnsureState(Process.State.HaveNtProcessInfo);
  616.         return this.processInfo.pageFileBytesPeak;
  617.       }
  618.     }
  619.  
  620.     /// <summary>Gets the peak working set size for the associated process, in bytes.</summary>
  621.     /// <returns>The maximum amount of physical memory that the associated process has required all at once, in bytes.</returns>
  622.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  623.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.PeakWorkingSet64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  624.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  625.     [MonitoringDescription("ProcessPeakWorkingSet")]
  626.     public int PeakWorkingSet
  627.     {
  628.       get
  629.       {
  630.         this.EnsureState(Process.State.HaveNtProcessInfo);
  631.         return (int) this.processInfo.workingSetPeak;
  632.       }
  633.     }
  634.  
  635.     /// <summary>Gets the maximum amount of physical memory, in bytes, used by the associated process.</summary>
  636.     /// <returns>The maximum amount of physical memory, in bytes, allocated for the associated process since it was started.</returns>
  637.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  638.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  639.     [MonitoringDescription("ProcessPeakWorkingSet")]
  640.     [ComVisible(false)]
  641.     public long PeakWorkingSet64
  642.     {
  643.       get
  644.       {
  645.         this.EnsureState(Process.State.HaveNtProcessInfo);
  646.         return this.processInfo.workingSetPeak;
  647.       }
  648.     }
  649.  
  650.     /// <summary>Gets the maximum amount of virtual memory, in bytes, used by the associated process.</summary>
  651.     /// <returns>The maximum amount of virtual memory, in bytes, that the associated process has requested.</returns>
  652.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  653.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.PeakVirtualMemorySize64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  654.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  655.     [MonitoringDescription("ProcessPeakVirtualMemorySize")]
  656.     public int PeakVirtualMemorySize
  657.     {
  658.       get
  659.       {
  660.         this.EnsureState(Process.State.HaveNtProcessInfo);
  661.         return (int) this.processInfo.virtualBytesPeak;
  662.       }
  663.     }
  664.  
  665.     /// <summary>Gets the maximum amount of virtual memory, in bytes, used by the associated process.</summary>
  666.     /// <returns>The maximum amount of virtual memory, in bytes, allocated for the associated process since it was started.</returns>
  667.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  668.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  669.     [MonitoringDescription("ProcessPeakVirtualMemorySize")]
  670.     [ComVisible(false)]
  671.     public long PeakVirtualMemorySize64
  672.     {
  673.       get
  674.       {
  675.         this.EnsureState(Process.State.HaveNtProcessInfo);
  676.         return this.processInfo.virtualBytesPeak;
  677.       }
  678.     }
  679.  
  680.     private OperatingSystem OperatingSystem
  681.     {
  682.       get
  683.       {
  684.         if (this.operatingSystem == null)
  685.           this.operatingSystem = Environment.OSVersion;
  686.         return this.operatingSystem;
  687.       }
  688.     }
  689.  
  690.     /// <summary>Gets or sets a value indicating whether the associated process priority should temporarily be boosted by the operating system when the main window has the focus.</summary>
  691.     /// <returns>
  692.     /// <see langword="true" /> if dynamic boosting of the process priority should take place for a process when it is taken out of the wait state; otherwise, <see langword="false" />. The default is <see langword="false" />.</returns>
  693.     /// <exception cref="T:System.ComponentModel.Win32Exception">Priority boost information could not be retrieved from the associated process resource. </exception>
  694.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.-or- The process identifier or process handle is zero. (The process has not been started.) </exception>
  695.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.PriorityBoostEnabled" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  696.     /// <exception cref="T:System.InvalidOperationException">The process <see cref="P:System.Diagnostics.Process.Id" /> is not available.</exception>
  697.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  698.     [MonitoringDescription("ProcessPriorityBoostEnabled")]
  699.     public bool PriorityBoostEnabled
  700.     {
  701.       get
  702.       {
  703.         this.EnsureState(Process.State.IsNt);
  704.         if (!this.havePriorityBoostEnabled)
  705.         {
  706.           SafeProcessHandle handle = (SafeProcessHandle) null;
  707.           try
  708.           {
  709.             handle = this.GetProcessHandle(1024);
  710.             bool disabled = false;
  711.             if (!Microsoft.Win32.NativeMethods.GetProcessPriorityBoost(handle, out disabled))
  712.               throw new Win32Exception();
  713.             this.priorityBoostEnabled = !disabled;
  714.             this.havePriorityBoostEnabled = true;
  715.           }
  716.           finally
  717.           {
  718.             this.ReleaseProcessHandle(handle);
  719.           }
  720.         }
  721.         return this.priorityBoostEnabled;
  722.       }
  723.       set
  724.       {
  725.         this.EnsureState(Process.State.IsNt);
  726.         SafeProcessHandle handle = (SafeProcessHandle) null;
  727.         try
  728.         {
  729.           handle = this.GetProcessHandle(512);
  730.           if (!Microsoft.Win32.NativeMethods.SetProcessPriorityBoost(handle, !value))
  731.             throw new Win32Exception();
  732.           this.priorityBoostEnabled = value;
  733.           this.havePriorityBoostEnabled = true;
  734.         }
  735.         finally
  736.         {
  737.           this.ReleaseProcessHandle(handle);
  738.         }
  739.       }
  740.     }
  741.  
  742.     /// <summary>Gets or sets the overall priority category for the associated process.</summary>
  743.     /// <returns>The priority category for the associated process, from which the <see cref="P:System.Diagnostics.Process.BasePriority" /> of the process is calculated.</returns>
  744.     /// <exception cref="T:System.ComponentModel.Win32Exception">Process priority information could not be set or retrieved from the associated process resource.-or- The process identifier or process handle is zero. (The process has not been started.) </exception>
  745.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.PriorityClass" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  746.     /// <exception cref="T:System.InvalidOperationException">The process <see cref="P:System.Diagnostics.Process.Id" /> is not available.</exception>
  747.     /// <exception cref="T:System.PlatformNotSupportedException">You have set the <see cref="P:System.Diagnostics.Process.PriorityClass" /> to <see langword="AboveNormal" /> or <see langword="BelowNormal" /> when using Windows 98 or Windows Millennium Edition (Windows Me). These platforms do not support those values for the priority class. </exception>
  748.     /// <exception cref="T:System.ComponentModel.InvalidEnumArgumentException">Priority class cannot be set because it does not use a valid value, as defined in the <see cref="T:System.Diagnostics.ProcessPriorityClass" /> enumeration.</exception>
  749.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  750.     [MonitoringDescription("ProcessPriorityClass")]
  751.     public ProcessPriorityClass PriorityClass
  752.     {
  753.       get
  754.       {
  755.         if (!this.havePriorityClass)
  756.         {
  757.           SafeProcessHandle handle = (SafeProcessHandle) null;
  758.           try
  759.           {
  760.             handle = this.GetProcessHandle(1024);
  761.             int priorityClass = Microsoft.Win32.NativeMethods.GetPriorityClass(handle);
  762.             if (priorityClass == 0)
  763.               throw new Win32Exception();
  764.             this.priorityClass = (ProcessPriorityClass) priorityClass;
  765.             this.havePriorityClass = true;
  766.           }
  767.           finally
  768.           {
  769.             this.ReleaseProcessHandle(handle);
  770.           }
  771.         }
  772.         return this.priorityClass;
  773.       }
  774.       set
  775.       {
  776.         if (!Enum.IsDefined(typeof (ProcessPriorityClass), (object) value))
  777.           throw new InvalidEnumArgumentException(nameof (value), (int) value, typeof (ProcessPriorityClass));
  778.         if ((value & (ProcessPriorityClass.BelowNormal | ProcessPriorityClass.AboveNormal)) != (ProcessPriorityClass) 0 && (this.OperatingSystem.Platform != PlatformID.Win32NT || this.OperatingSystem.Version.Major < 5))
  779.           throw new PlatformNotSupportedException(SR.GetString("PriorityClassNotSupported"), (Exception) null);
  780.         SafeProcessHandle handle = (SafeProcessHandle) null;
  781.         try
  782.         {
  783.           handle = this.GetProcessHandle(512);
  784.           if (!Microsoft.Win32.NativeMethods.SetPriorityClass(handle, (int) value))
  785.             throw new Win32Exception();
  786.           this.priorityClass = value;
  787.           this.havePriorityClass = true;
  788.         }
  789.         finally
  790.         {
  791.           this.ReleaseProcessHandle(handle);
  792.         }
  793.       }
  794.     }
  795.  
  796.     /// <summary>Gets the amount of private memory, in bytes, allocated for the associated process.</summary>
  797.     /// <returns>The number of bytes allocated by the associated process that cannot be shared with other processes.</returns>
  798.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  799.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.PrivateMemorySize64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  800.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  801.     [MonitoringDescription("ProcessPrivateMemorySize")]
  802.     public int PrivateMemorySize
  803.     {
  804.       get
  805.       {
  806.         this.EnsureState(Process.State.HaveNtProcessInfo);
  807.         return (int) this.processInfo.privateBytes;
  808.       }
  809.     }
  810.  
  811.     /// <summary>Gets the amount of private memory, in bytes, allocated for the associated process.</summary>
  812.     /// <returns>The amount of memory, in bytes, allocated for the associated process that cannot be shared with other processes.</returns>
  813.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  814.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  815.     [MonitoringDescription("ProcessPrivateMemorySize")]
  816.     [ComVisible(false)]
  817.     public long PrivateMemorySize64
  818.     {
  819.       get
  820.       {
  821.         this.EnsureState(Process.State.HaveNtProcessInfo);
  822.         return this.processInfo.privateBytes;
  823.       }
  824.     }
  825.  
  826.     /// <summary>Gets the privileged processor time for this process.</summary>
  827.     /// <returns>A <see cref="T:System.TimeSpan" /> that indicates the amount of time that the process has spent running code inside the operating system core.</returns>
  828.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  829.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.PrivilegedProcessorTime" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  830.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  831.     [MonitoringDescription("ProcessPrivilegedProcessorTime")]
  832.     public TimeSpan PrivilegedProcessorTime
  833.     {
  834.       get
  835.       {
  836.         this.EnsureState(Process.State.IsNt);
  837.         return this.GetProcessTimes().PrivilegedProcessorTime;
  838.       }
  839.     }
  840.  
  841.     /// <summary>Gets the name of the process.</summary>
  842.     /// <returns>The name that the system uses to identify the process to the user.</returns>
  843.     /// <exception cref="T:System.InvalidOperationException">The process does not have an identifier, or no process is associated with the <see cref="T:System.Diagnostics.Process" />.-or- The associated process has exited. </exception>
  844.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  845.     /// <exception cref="T:System.NotSupportedException">The process is not on this computer.</exception>
  846.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  847.     [MonitoringDescription("ProcessProcessName")]
  848.     public string ProcessName
  849.     {
  850.       get
  851.       {
  852.         this.EnsureState(Process.State.HaveProcessInfo);
  853.         if (this.processInfo.processName.Length == 15 && ProcessManager.IsNt && ProcessManager.IsOSOlderThanXP)
  854.         {
  855.           if (!this.isRemoteMachine)
  856.           {
  857.             try
  858.             {
  859.               string moduleName = this.MainModule.ModuleName;
  860.               if (moduleName != null)
  861.                 this.processInfo.processName = Path.ChangeExtension(Path.GetFileName(moduleName), (string) null);
  862.             }
  863.             catch (Exception ex)
  864.             {
  865.             }
  866.           }
  867.         }
  868.         return this.processInfo.processName;
  869.       }
  870.     }
  871.  
  872.     /// <summary>Gets or sets the processors on which the threads in this process can be scheduled to run.</summary>
  873.     /// <returns>A bitmask representing the processors that the threads in the associated process can run on. The default depends on the number of processors on the computer. The default value is 2 n -1, where n is the number of processors.</returns>
  874.     /// <exception cref="T:System.ComponentModel.Win32Exception">
  875.     /// <see cref="P:System.Diagnostics.Process.ProcessorAffinity" /> information could not be set or retrieved from the associated process resource.-or- The process identifier or process handle is zero. (The process has not been started.) </exception>
  876.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.ProcessorAffinity" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  877.     /// <exception cref="T:System.InvalidOperationException">The process <see cref="P:System.Diagnostics.Process.Id" /> was not available.-or- The process has exited. </exception>
  878.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  879.     [MonitoringDescription("ProcessProcessorAffinity")]
  880.     public IntPtr ProcessorAffinity
  881.     {
  882.       get
  883.       {
  884.         if (!this.haveProcessorAffinity)
  885.         {
  886.           SafeProcessHandle handle = (SafeProcessHandle) null;
  887.           try
  888.           {
  889.             handle = this.GetProcessHandle(1024);
  890.             IntPtr processMask;
  891.             IntPtr systemMask;
  892.             if (!Microsoft.Win32.NativeMethods.GetProcessAffinityMask(handle, out processMask, out systemMask))
  893.               throw new Win32Exception();
  894.             this.processorAffinity = processMask;
  895.           }
  896.           finally
  897.           {
  898.             this.ReleaseProcessHandle(handle);
  899.           }
  900.           this.haveProcessorAffinity = true;
  901.         }
  902.         return this.processorAffinity;
  903.       }
  904.       set
  905.       {
  906.         SafeProcessHandle handle = (SafeProcessHandle) null;
  907.         try
  908.         {
  909.           handle = this.GetProcessHandle(512);
  910.           if (!Microsoft.Win32.NativeMethods.SetProcessAffinityMask(handle, value))
  911.             throw new Win32Exception();
  912.           this.processorAffinity = value;
  913.           this.haveProcessorAffinity = true;
  914.         }
  915.         finally
  916.         {
  917.           this.ReleaseProcessHandle(handle);
  918.         }
  919.       }
  920.     }
  921.  
  922.     /// <summary>Gets a value indicating whether the user interface of the process is responding.</summary>
  923.     /// <returns>
  924.     /// <see langword="true" /> if the user interface of the associated process is responding to the system; otherwise, <see langword="false" />.</returns>
  925.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  926.     /// <exception cref="T:System.InvalidOperationException">There is no process associated with this <see cref="T:System.Diagnostics.Process" /> object. </exception>
  927.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.Responding" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  928.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  929.     [MonitoringDescription("ProcessResponding")]
  930.     public bool Responding
  931.     {
  932.       get
  933.       {
  934.         if (!this.haveResponding)
  935.         {
  936.           IntPtr mainWindowHandle = this.MainWindowHandle;
  937.           IntPtr pdwResult;
  938.           this.responding = mainWindowHandle == (IntPtr) 0 || Microsoft.Win32.NativeMethods.SendMessageTimeout(new HandleRef((object) this, mainWindowHandle), 0, IntPtr.Zero, IntPtr.Zero, 2, 5000, out pdwResult) != (IntPtr) 0;
  939.         }
  940.         return this.responding;
  941.       }
  942.     }
  943.  
  944.     /// <summary>Gets the Terminal Services session identifier for the associated process.</summary>
  945.     /// <returns>The Terminal Services session identifier for the associated process.</returns>
  946.     /// <exception cref="T:System.NullReferenceException">There is no session associated with this process.</exception>
  947.     /// <exception cref="T:System.InvalidOperationException">There is no process associated with this session identifier.-or-The associated process is not on this machine. </exception>
  948.     /// <exception cref="T:System.PlatformNotSupportedException">The <see cref="P:System.Diagnostics.Process.SessionId" /> property is not supported on Windows 98.</exception>
  949.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  950.     [MonitoringDescription("ProcessSessionId")]
  951.     public int SessionId
  952.     {
  953.       get
  954.       {
  955.         this.EnsureState(Process.State.HaveNtProcessInfo);
  956.         return this.processInfo.sessionId;
  957.       }
  958.     }
  959.  
  960.     /// <summary>Gets or sets the properties to pass to the <see cref="M:System.Diagnostics.Process.Start" /> method of the <see cref="T:System.Diagnostics.Process" />.</summary>
  961.     /// <returns>The <see cref="T:System.Diagnostics.ProcessStartInfo" /> that represents the data with which to start the process. These arguments include the name of the executable file or document used to start the process.</returns>
  962.     /// <exception cref="T:System.ArgumentNullException">The value that specifies the <see cref="P:System.Diagnostics.Process.StartInfo" /> is <see langword="null" />. </exception>
  963.     [Browsable(false)]
  964.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  965.     [MonitoringDescription("ProcessStartInfo")]
  966.     public ProcessStartInfo StartInfo
  967.     {
  968.       get
  969.       {
  970.         if (this.startInfo == null)
  971.           this.startInfo = new ProcessStartInfo(this);
  972.         return this.startInfo;
  973.       }
  974.       set
  975.       {
  976.         if (value == null)
  977.           throw new ArgumentNullException(nameof (value));
  978.         this.startInfo = value;
  979.       }
  980.     }
  981.  
  982.     /// <summary>Gets the time that the associated process was started.</summary>
  983.     /// <returns>An object  that indicates when the process started. An exception is thrown if the process is not running.</returns>
  984.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  985.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.StartTime" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  986.     /// <exception cref="T:System.InvalidOperationException">The process has exited.-or-The process has not been started.</exception>
  987.     /// <exception cref="T:System.ComponentModel.Win32Exception">An error occurred in the call to the Windows function.</exception>
  988.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  989.     [MonitoringDescription("ProcessStartTime")]
  990.     public DateTime StartTime
  991.     {
  992.       get
  993.       {
  994.         this.EnsureState(Process.State.IsNt);
  995.         return this.GetProcessTimes().StartTime;
  996.       }
  997.     }
  998.  
  999.     /// <summary>Gets or sets the object used to marshal the event handler calls that are issued as a result of a process exit event.</summary>
  1000.     /// <returns>The <see cref="T:System.ComponentModel.ISynchronizeInvoke" /> used to marshal event handler calls that are issued as a result of an <see cref="E:System.Diagnostics.Process.Exited" /> event on the process.</returns>
  1001.     [Browsable(false)]
  1002.     [DefaultValue(null)]
  1003.     [MonitoringDescription("ProcessSynchronizingObject")]
  1004.     public ISynchronizeInvoke SynchronizingObject
  1005.     {
  1006.       get
  1007.       {
  1008.         if (this.synchronizingObject == null && this.DesignMode)
  1009.         {
  1010.           IDesignerHost service = (IDesignerHost) this.GetService(typeof (IDesignerHost));
  1011.           if (service != null)
  1012.           {
  1013.             object rootComponent = (object) service.RootComponent;
  1014.             if (rootComponent != null && rootComponent is ISynchronizeInvoke)
  1015.               this.synchronizingObject = (ISynchronizeInvoke) rootComponent;
  1016.           }
  1017.         }
  1018.         return this.synchronizingObject;
  1019.       }
  1020.       set
  1021.       {
  1022.         this.synchronizingObject = value;
  1023.       }
  1024.     }
  1025.  
  1026.     /// <summary>Gets the set of threads that are running in the associated process.</summary>
  1027.     /// <returns>An array of type <see cref="T:System.Diagnostics.ProcessThread" /> representing the operating system threads currently running in the associated process.</returns>
  1028.     /// <exception cref="T:System.SystemException">The process does not have an <see cref="P:System.Diagnostics.Process.Id" />, or no process is associated with the <see cref="T:System.Diagnostics.Process" /> instance.-or- The associated process has exited. </exception>
  1029.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  1030.     [Browsable(false)]
  1031.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1032.     [MonitoringDescription("ProcessThreads")]
  1033.     public ProcessThreadCollection Threads
  1034.     {
  1035.       get
  1036.       {
  1037.         if (this.threads == null)
  1038.         {
  1039.           this.EnsureState(Process.State.HaveProcessInfo);
  1040.           int count = this.processInfo.threadInfoList.Count;
  1041.           ProcessThread[] processThreads = new ProcessThread[count];
  1042.           for (int index = 0; index < count; ++index)
  1043.             processThreads[index] = new ProcessThread(this.isRemoteMachine, (ThreadInfo) this.processInfo.threadInfoList[index]);
  1044.           this.threads = new ProcessThreadCollection(processThreads);
  1045.         }
  1046.         return this.threads;
  1047.       }
  1048.     }
  1049.  
  1050.     /// <summary>Gets the total processor time for this process.</summary>
  1051.     /// <returns>A <see cref="T:System.TimeSpan" /> that indicates the amount of time that the associated process has spent utilizing the CPU. This value is the sum of the <see cref="P:System.Diagnostics.Process.UserProcessorTime" /> and the <see cref="P:System.Diagnostics.Process.PrivilegedProcessorTime" />.</returns>
  1052.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  1053.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.TotalProcessorTime" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  1054.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1055.     [MonitoringDescription("ProcessTotalProcessorTime")]
  1056.     public TimeSpan TotalProcessorTime
  1057.     {
  1058.       get
  1059.       {
  1060.         this.EnsureState(Process.State.IsNt);
  1061.         return this.GetProcessTimes().TotalProcessorTime;
  1062.       }
  1063.     }
  1064.  
  1065.     /// <summary>Gets the user processor time for this process.</summary>
  1066.     /// <returns>A <see cref="T:System.TimeSpan" /> that indicates the amount of time that the associated process has spent running code inside the application portion of the process (not inside the operating system core).</returns>
  1067.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  1068.     /// <exception cref="T:System.NotSupportedException">You are attempting to access the <see cref="P:System.Diagnostics.Process.UserProcessorTime" /> property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer. </exception>
  1069.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1070.     [MonitoringDescription("ProcessUserProcessorTime")]
  1071.     public TimeSpan UserProcessorTime
  1072.     {
  1073.       get
  1074.       {
  1075.         this.EnsureState(Process.State.IsNt);
  1076.         return this.GetProcessTimes().UserProcessorTime;
  1077.       }
  1078.     }
  1079.  
  1080.     /// <summary>Gets the size of the process's virtual memory, in bytes.</summary>
  1081.     /// <returns>The amount of virtual memory, in bytes, that the associated process has requested.</returns>
  1082.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  1083.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.VirtualMemorySize64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  1084.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1085.     [MonitoringDescription("ProcessVirtualMemorySize")]
  1086.     public int VirtualMemorySize
  1087.     {
  1088.       get
  1089.       {
  1090.         this.EnsureState(Process.State.HaveNtProcessInfo);
  1091.         return (int) this.processInfo.virtualBytes;
  1092.       }
  1093.     }
  1094.  
  1095.     /// <summary>Gets the amount of the virtual memory, in bytes, allocated for the associated process.</summary>
  1096.     /// <returns>The amount of virtual memory, in bytes, allocated for the associated process.</returns>
  1097.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  1098.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1099.     [MonitoringDescription("ProcessVirtualMemorySize")]
  1100.     [ComVisible(false)]
  1101.     public long VirtualMemorySize64
  1102.     {
  1103.       get
  1104.       {
  1105.         this.EnsureState(Process.State.HaveNtProcessInfo);
  1106.         return this.processInfo.virtualBytes;
  1107.       }
  1108.     }
  1109.  
  1110.     /// <summary>Gets or sets whether the <see cref="E:System.Diagnostics.Process.Exited" /> event should be raised when the process terminates.</summary>
  1111.     /// <returns>
  1112.     /// <see langword="true" /> if the <see cref="E:System.Diagnostics.Process.Exited" /> event should be raised when the associated process is terminated (through either an exit or a call to <see cref="M:System.Diagnostics.Process.Kill" />); otherwise, <see langword="false" />. The default is <see langword="false" />.</returns>
  1113.     [Browsable(false)]
  1114.     [DefaultValue(false)]
  1115.     [MonitoringDescription("ProcessEnableRaisingEvents")]
  1116.     public bool EnableRaisingEvents
  1117.     {
  1118.       get
  1119.       {
  1120.         return this.watchForExit;
  1121.       }
  1122.       set
  1123.       {
  1124.         if (value == this.watchForExit)
  1125.           return;
  1126.         if (this.Associated)
  1127.         {
  1128.           if (value)
  1129.           {
  1130.             this.OpenProcessHandle();
  1131.             this.EnsureWatchingForExit();
  1132.           }
  1133.           else
  1134.             this.StopWatchingForExit();
  1135.         }
  1136.         this.watchForExit = value;
  1137.       }
  1138.     }
  1139.  
  1140.     /// <summary>Gets a stream used to write the input of the application.</summary>
  1141.     /// <returns>A <see cref="T:System.IO.StreamWriter" /> that can be used to write the standard input stream of the application.</returns>
  1142.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.Process.StandardInput" /> stream has not been defined because <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardInput" /> is set to <see langword="false" />. </exception>
  1143.     [Browsable(false)]
  1144.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1145.     [MonitoringDescription("ProcessStandardInput")]
  1146.     public StreamWriter StandardInput
  1147.     {
  1148.       get
  1149.       {
  1150.         if (this.standardInput == null)
  1151.           throw new InvalidOperationException(SR.GetString("CantGetStandardIn"));
  1152.         return this.standardInput;
  1153.       }
  1154.     }
  1155.  
  1156.     /// <summary>Gets a stream used to read the textual output of the application.</summary>
  1157.     /// <returns>A <see cref="T:System.IO.StreamReader" /> that can be used to read the standard output stream of the application.</returns>
  1158.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream has not been defined for redirection; ensure <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardOutput" /> is set to <see langword="true" /> and <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> is set to <see langword="false" />.- or - The <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream has been opened for asynchronous read operations with <see cref="M:System.Diagnostics.Process.BeginOutputReadLine" />. </exception>
  1159.     [Browsable(false)]
  1160.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1161.     [MonitoringDescription("ProcessStandardOutput")]
  1162.     public StreamReader StandardOutput
  1163.     {
  1164.       get
  1165.       {
  1166.         if (this.standardOutput == null)
  1167.           throw new InvalidOperationException(SR.GetString("CantGetStandardOut"));
  1168.         if (this.outputStreamReadMode == Process.StreamReadMode.undefined)
  1169.           this.outputStreamReadMode = Process.StreamReadMode.syncMode;
  1170.         else if (this.outputStreamReadMode != Process.StreamReadMode.syncMode)
  1171.           throw new InvalidOperationException(SR.GetString("CantMixSyncAsyncOperation"));
  1172.         return this.standardOutput;
  1173.       }
  1174.     }
  1175.  
  1176.     /// <summary>Gets a stream used to read the error output of the application.</summary>
  1177.     /// <returns>A <see cref="T:System.IO.StreamReader" /> that can be used to read the standard error stream of the application.</returns>
  1178.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.Process.StandardError" /> stream has not been defined for redirection; ensure <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardError" /> is set to <see langword="true" /> and <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> is set to <see langword="false" />.- or - The <see cref="P:System.Diagnostics.Process.StandardError" /> stream has been opened for asynchronous read operations with <see cref="M:System.Diagnostics.Process.BeginErrorReadLine" />. </exception>
  1179.     [Browsable(false)]
  1180.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1181.     [MonitoringDescription("ProcessStandardError")]
  1182.     public StreamReader StandardError
  1183.     {
  1184.       get
  1185.       {
  1186.         if (this.standardError == null)
  1187.           throw new InvalidOperationException(SR.GetString("CantGetStandardError"));
  1188.         if (this.errorStreamReadMode == Process.StreamReadMode.undefined)
  1189.           this.errorStreamReadMode = Process.StreamReadMode.syncMode;
  1190.         else if (this.errorStreamReadMode != Process.StreamReadMode.syncMode)
  1191.           throw new InvalidOperationException(SR.GetString("CantMixSyncAsyncOperation"));
  1192.         return this.standardError;
  1193.       }
  1194.     }
  1195.  
  1196.     /// <summary>Gets the associated process's physical memory usage, in bytes.</summary>
  1197.     /// <returns>The total amount of physical memory the associated process is using, in bytes.</returns>
  1198.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property. </exception>
  1199.     [Obsolete("This property has been deprecated.  Please use System.Diagnostics.Process.WorkingSet64 instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
  1200.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1201.     [MonitoringDescription("ProcessWorkingSet")]
  1202.     public int WorkingSet
  1203.     {
  1204.       get
  1205.       {
  1206.         this.EnsureState(Process.State.HaveNtProcessInfo);
  1207.         return (int) this.processInfo.workingSet;
  1208.       }
  1209.     }
  1210.  
  1211.     /// <summary>Gets the amount of physical memory, in bytes, allocated for the associated process.</summary>
  1212.     /// <returns>The amount of physical memory, in bytes, allocated for the associated process.</returns>
  1213.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me), which does not support this property.</exception>
  1214.     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1215.     [MonitoringDescription("ProcessWorkingSet")]
  1216.     [ComVisible(false)]
  1217.     public long WorkingSet64
  1218.     {
  1219.       get
  1220.       {
  1221.         this.EnsureState(Process.State.HaveNtProcessInfo);
  1222.         return this.processInfo.workingSet;
  1223.       }
  1224.     }
  1225.  
  1226.     /// <summary>Occurs when a process exits.</summary>
  1227.     [Category("Behavior")]
  1228.     [MonitoringDescription("ProcessExited")]
  1229.     public event EventHandler Exited
  1230.     {
  1231.       add
  1232.       {
  1233.         this.onExited += value;
  1234.       }
  1235.       remove
  1236.       {
  1237.         this.onExited -= value;
  1238.       }
  1239.     }
  1240.  
  1241.     /// <summary>Closes a process that has a user interface by sending a close message to its main window.</summary>
  1242.     /// <returns>
  1243.     /// <see langword="true" /> if the close message was successfully sent; <see langword="false" /> if the associated process does not have a main window or if the main window is disabled (for example if a modal dialog is being shown).</returns>
  1244.     /// <exception cref="T:System.PlatformNotSupportedException">The platform is Windows 98 or Windows Millennium Edition (Windows Me); set the <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> property to <see langword="false" /> to access this property on Windows 98 and Windows Me.</exception>
  1245.     /// <exception cref="T:System.InvalidOperationException">The process has already exited. -or-No process is associated with this <see cref="T:System.Diagnostics.Process" /> object.</exception>
  1246.     public bool CloseMainWindow()
  1247.     {
  1248.       IntPtr mainWindowHandle = this.MainWindowHandle;
  1249.       if (mainWindowHandle == (IntPtr) 0 || (Microsoft.Win32.NativeMethods.GetWindowLong(new HandleRef((object) this, mainWindowHandle), -16) & 134217728) != 0)
  1250.         return false;
  1251.       Microsoft.Win32.NativeMethods.PostMessage(new HandleRef((object) this, mainWindowHandle), 16, IntPtr.Zero, IntPtr.Zero);
  1252.       return true;
  1253.     }
  1254.  
  1255.     private void ReleaseProcessHandle(SafeProcessHandle handle)
  1256.     {
  1257.       if (handle == null || this.haveProcessHandle && handle == this.m_processHandle)
  1258.         return;
  1259.       handle.Close();
  1260.     }
  1261.  
  1262.     private void CompletionCallback(object context, bool wasSignaled)
  1263.     {
  1264.       this.StopWatchingForExit();
  1265.       this.RaiseOnExited();
  1266.     }
  1267.  
  1268.     /// <summary>Release all resources used by this process.</summary>
  1269.     /// <param name="disposing">
  1270.     /// <see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources. </param>
  1271.     protected override void Dispose(bool disposing)
  1272.     {
  1273.       if (this.disposed)
  1274.         return;
  1275.       if (disposing)
  1276.         this.Close();
  1277.       this.disposed = true;
  1278.       base.Dispose(disposing);
  1279.     }
  1280.  
  1281.     /// <summary>Frees all the resources that are associated with this component.</summary>
  1282.     public void Close()
  1283.     {
  1284.       if (!this.Associated)
  1285.         return;
  1286.       if (this.haveProcessHandle)
  1287.       {
  1288.         this.StopWatchingForExit();
  1289.         this.m_processHandle.Close();
  1290.         this.m_processHandle = (SafeProcessHandle) null;
  1291.         this.haveProcessHandle = false;
  1292.       }
  1293.       this.haveProcessId = false;
  1294.       this.isRemoteMachine = false;
  1295.       this.machineName = ".";
  1296.       this.raisedOnExited = false;
  1297.       this.standardOutput = (StreamReader) null;
  1298.       this.standardInput = (StreamWriter) null;
  1299.       this.standardError = (StreamReader) null;
  1300.       this.output = (AsyncStreamReader) null;
  1301.       this.error = (AsyncStreamReader) null;
  1302.       this.Refresh();
  1303.     }
  1304.  
  1305.     private void EnsureState(Process.State state)
  1306.     {
  1307.       if ((state & Process.State.IsWin2k) != (Process.State) 0 && (this.OperatingSystem.Platform != PlatformID.Win32NT || this.OperatingSystem.Version.Major < 5))
  1308.         throw new PlatformNotSupportedException(SR.GetString("Win2kRequired"));
  1309.       if ((state & Process.State.IsNt) != (Process.State) 0 && this.OperatingSystem.Platform != PlatformID.Win32NT)
  1310.         throw new PlatformNotSupportedException(SR.GetString("WinNTRequired"));
  1311.       if ((state & Process.State.Associated) != (Process.State) 0 && !this.Associated)
  1312.         throw new InvalidOperationException(SR.GetString("NoAssociatedProcess"));
  1313.       if ((state & Process.State.HaveId) != (Process.State) 0 && !this.haveProcessId)
  1314.       {
  1315.         if (this.haveProcessHandle)
  1316.         {
  1317.           this.SetProcessId(ProcessManager.GetProcessIdFromHandle(this.m_processHandle));
  1318.         }
  1319.         else
  1320.         {
  1321.           this.EnsureState(Process.State.Associated);
  1322.           throw new InvalidOperationException(SR.GetString("ProcessIdRequired"));
  1323.         }
  1324.       }
  1325.       if ((state & Process.State.IsLocal) != (Process.State) 0 && this.isRemoteMachine)
  1326.         throw new NotSupportedException(SR.GetString("NotSupportedRemote"));
  1327.       if ((state & Process.State.HaveProcessInfo) != (Process.State) 0 && this.processInfo == null)
  1328.       {
  1329.         if ((state & Process.State.HaveId) == (Process.State) 0)
  1330.           this.EnsureState(Process.State.HaveId);
  1331.         this.processInfo = ProcessManager.GetProcessInfo(this.processId, this.machineName);
  1332.         if (this.processInfo == null)
  1333.           throw new InvalidOperationException(SR.GetString("NoProcessInfo"));
  1334.       }
  1335.       if ((state & Process.State.Exited) == (Process.State) 0)
  1336.         return;
  1337.       if (!this.HasExited)
  1338.         throw new InvalidOperationException(SR.GetString("WaitTillExit"));
  1339.       if (!this.haveProcessHandle)
  1340.         throw new InvalidOperationException(SR.GetString("NoProcessHandle"));
  1341.     }
  1342.  
  1343.     private void EnsureWatchingForExit()
  1344.     {
  1345.       if (this.watchingForExit)
  1346.         return;
  1347.       lock (this)
  1348.       {
  1349.         if (this.watchingForExit)
  1350.           return;
  1351.         this.watchingForExit = true;
  1352.         try
  1353.         {
  1354.           this.waitHandle = (WaitHandle) new ProcessWaitHandle(this.m_processHandle);
  1355.           this.registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(this.waitHandle, new WaitOrTimerCallback(this.CompletionCallback), (object) null, -1, true);
  1356.         }
  1357.         catch
  1358.         {
  1359.           this.watchingForExit = false;
  1360.           throw;
  1361.         }
  1362.       }
  1363.     }
  1364.  
  1365.     private void EnsureWorkingSetLimits()
  1366.     {
  1367.       this.EnsureState(Process.State.IsNt);
  1368.       if (this.haveWorkingSetLimits)
  1369.         return;
  1370.       SafeProcessHandle handle = (SafeProcessHandle) null;
  1371.       try
  1372.       {
  1373.         handle = this.GetProcessHandle(1024);
  1374.         IntPtr min;
  1375.         IntPtr max;
  1376.         if (!Microsoft.Win32.NativeMethods.GetProcessWorkingSetSize(handle, out min, out max))
  1377.           throw new Win32Exception();
  1378.         this.minWorkingSet = min;
  1379.         this.maxWorkingSet = max;
  1380.         this.haveWorkingSetLimits = true;
  1381.       }
  1382.       finally
  1383.       {
  1384.         this.ReleaseProcessHandle(handle);
  1385.       }
  1386.     }
  1387.  
  1388.     /// <summary>Puts a <see cref="T:System.Diagnostics.Process" /> component in state to interact with operating system processes that run in a special mode by enabling the native property <see langword="SeDebugPrivilege" /> on the current thread.</summary>
  1389.     public static void EnterDebugMode()
  1390.     {
  1391.       if (!ProcessManager.IsNt)
  1392.         return;
  1393.       Process.SetPrivilege("SeDebugPrivilege", 2);
  1394.     }
  1395.  
  1396.     private static void SetPrivilege(string privilegeName, int attrib)
  1397.     {
  1398.       IntPtr TokenHandle = (IntPtr) 0;
  1399.       Microsoft.Win32.NativeMethods.LUID lpLuid = new Microsoft.Win32.NativeMethods.LUID();
  1400.       if (!Microsoft.Win32.NativeMethods.OpenProcessToken(new HandleRef((object) null, Microsoft.Win32.NativeMethods.GetCurrentProcess()), 32, out TokenHandle))
  1401.         throw new Win32Exception();
  1402.       try
  1403.       {
  1404.         if (!Microsoft.Win32.NativeMethods.LookupPrivilegeValue((string) null, privilegeName, out lpLuid))
  1405.           throw new Win32Exception();
  1406.         Microsoft.Win32.NativeMethods.AdjustTokenPrivileges(new HandleRef((object) null, TokenHandle), false, new Microsoft.Win32.NativeMethods.TokenPrivileges()
  1407.         {
  1408.           Luid = lpLuid,
  1409.           Attributes = attrib
  1410.         }, 0, IntPtr.Zero, IntPtr.Zero);
  1411.         if (Marshal.GetLastWin32Error() != 0)
  1412.           throw new Win32Exception();
  1413.       }
  1414.       finally
  1415.       {
  1416.         SafeNativeMethods.CloseHandle(TokenHandle);
  1417.       }
  1418.     }
  1419.  
  1420.     /// <summary>Takes a <see cref="T:System.Diagnostics.Process" /> component out of the state that lets it interact with operating system processes that run in a special mode.</summary>
  1421.     public static void LeaveDebugMode()
  1422.     {
  1423.       if (!ProcessManager.IsNt)
  1424.         return;
  1425.       Process.SetPrivilege("SeDebugPrivilege", 0);
  1426.     }
  1427.  
  1428.     /// <summary>Returns a new <see cref="T:System.Diagnostics.Process" /> component, given a process identifier and the name of a computer on the network.</summary>
  1429.     /// <param name="processId">The system-unique identifier of a process resource. </param>
  1430.     /// <param name="machineName">The name of a computer on the network. </param>
  1431.     /// <returns>A <see cref="T:System.Diagnostics.Process" /> component that is associated with a remote process resource identified by the <paramref name="processId" /> parameter.</returns>
  1432.     /// <exception cref="T:System.ArgumentException">The process specified by the <paramref name="processId" /> parameter is not running. The identifier might be expired.-or- The <paramref name="machineName" /> parameter syntax is invalid. The name might have length zero (0). </exception>
  1433.     /// <exception cref="T:System.ArgumentNullException">The <paramref name="machineName" /> parameter is <see langword="null" />. </exception>
  1434.     /// <exception cref="T:System.InvalidOperationException">The process was not started by this object.</exception>
  1435.     public static Process GetProcessById(int processId, string machineName)
  1436.     {
  1437.       if (!ProcessManager.IsProcessRunning(processId, machineName))
  1438.         throw new ArgumentException(SR.GetString("MissingProccess", (object) processId.ToString((IFormatProvider) CultureInfo.CurrentCulture)));
  1439.       return new Process(machineName, ProcessManager.IsRemoteMachine(machineName), processId, (ProcessInfo) null);
  1440.     }
  1441.  
  1442.     /// <summary>Returns a new <see cref="T:System.Diagnostics.Process" /> component, given the identifier of a process on the local computer.</summary>
  1443.     /// <param name="processId">The system-unique identifier of a process resource. </param>
  1444.     /// <returns>A <see cref="T:System.Diagnostics.Process" /> component that is associated with the local process resource identified by the <paramref name="processId" /> parameter.</returns>
  1445.     /// <exception cref="T:System.ArgumentException">The process specified by the <paramref name="processId" /> parameter is not running. The identifier might be expired. </exception>
  1446.     /// <exception cref="T:System.InvalidOperationException">The process was not started by this object.</exception>
  1447.     public static Process GetProcessById(int processId)
  1448.     {
  1449.       return Process.GetProcessById(processId, ".");
  1450.     }
  1451.  
  1452.     /// <summary>Creates an array of new <see cref="T:System.Diagnostics.Process" /> components and associates them with all the process resources on the local computer that share the specified process name.</summary>
  1453.     /// <param name="processName">The friendly name of the process. </param>
  1454.     /// <returns>An array of type <see cref="T:System.Diagnostics.Process" /> that represents the process resources running the specified application or file.</returns>
  1455.     /// <exception cref="T:System.InvalidOperationException">There are problems accessing the performance counter API's used to get process information. This exception is specific to Windows NT, Windows 2000, and Windows XP. </exception>
  1456.     public static Process[] GetProcessesByName(string processName)
  1457.     {
  1458.       return Process.GetProcessesByName(processName, ".");
  1459.     }
  1460.  
  1461.     /// <summary>Creates an array of new <see cref="T:System.Diagnostics.Process" /> components and associates them with all the process resources on a remote computer that share the specified process name.</summary>
  1462.     /// <param name="processName">The friendly name of the process. </param>
  1463.     /// <param name="machineName">The name of a computer on the network. </param>
  1464.     /// <returns>An array of type <see cref="T:System.Diagnostics.Process" /> that represents the process resources running the specified application or file.</returns>
  1465.     /// <exception cref="T:System.ArgumentException">The <paramref name="machineName" /> parameter syntax is invalid. It might have length zero (0). </exception>
  1466.     /// <exception cref="T:System.ArgumentNullException">The <paramref name="machineName" /> parameter is <see langword="null" />. </exception>
  1467.     /// <exception cref="T:System.PlatformNotSupportedException">The operating system platform does not support this operation on remote computers. </exception>
  1468.     /// <exception cref="T:System.InvalidOperationException">There are problems accessing the performance counter API's used to get process information. This exception is specific to Windows NT, Windows 2000, and Windows XP. </exception>
  1469.     /// <exception cref="T:System.ComponentModel.Win32Exception">A problem occurred accessing an underlying system API. </exception>
  1470.     public static Process[] GetProcessesByName(string processName, string machineName)
  1471.     {
  1472.       if (processName == null)
  1473.         processName = string.Empty;
  1474.       Process[] processes = Process.GetProcesses(machineName);
  1475.       ArrayList arrayList = new ArrayList();
  1476.       for (int index = 0; index < processes.Length; ++index)
  1477.       {
  1478.         if (string.Equals(processName, processes[index].ProcessName, StringComparison.OrdinalIgnoreCase))
  1479.           arrayList.Add((object) processes[index]);
  1480.         else
  1481.           processes[index].Dispose();
  1482.       }
  1483.       Process[] processArray = new Process[arrayList.Count];
  1484.       arrayList.CopyTo((Array) processArray, 0);
  1485.       return processArray;
  1486.     }
  1487.  
  1488.     /// <summary>Creates a new <see cref="T:System.Diagnostics.Process" /> component for each process resource on the local computer.</summary>
  1489.     /// <returns>An array of type <see cref="T:System.Diagnostics.Process" /> that represents all the process resources running on the local computer.</returns>
  1490.     public static Process[] GetProcesses()
  1491.     {
  1492.       return Process.GetProcesses(".");
  1493.     }
  1494.  
  1495.     /// <summary>Creates a new <see cref="T:System.Diagnostics.Process" /> component for each process resource on the specified computer.</summary>
  1496.     /// <param name="machineName">The computer from which to read the list of processes. </param>
  1497.     /// <returns>An array of type <see cref="T:System.Diagnostics.Process" /> that represents all the process resources running on the specified computer.</returns>
  1498.     /// <exception cref="T:System.ArgumentException">The <paramref name="machineName" /> parameter syntax is invalid. It might have length zero (0). </exception>
  1499.     /// <exception cref="T:System.ArgumentNullException">The <paramref name="machineName" /> parameter is <see langword="null" />. </exception>
  1500.     /// <exception cref="T:System.PlatformNotSupportedException">The operating system platform does not support this operation on remote computers. </exception>
  1501.     /// <exception cref="T:System.InvalidOperationException">There are problems accessing the performance counter API's used to get process information. This exception is specific to Windows NT, Windows 2000, and Windows XP. </exception>
  1502.     /// <exception cref="T:System.ComponentModel.Win32Exception">A problem occurred accessing an underlying system API. </exception>
  1503.     public static Process[] GetProcesses(string machineName)
  1504.     {
  1505.       bool isRemoteMachine = ProcessManager.IsRemoteMachine(machineName);
  1506.       ProcessInfo[] processInfos = ProcessManager.GetProcessInfos(machineName);
  1507.       Process[] processArray = new Process[processInfos.Length];
  1508.       for (int index = 0; index < processInfos.Length; ++index)
  1509.       {
  1510.         ProcessInfo processInfo = processInfos[index];
  1511.         processArray[index] = new Process(machineName, isRemoteMachine, processInfo.processId, processInfo);
  1512.       }
  1513.       return processArray;
  1514.     }
  1515.  
  1516.     /// <summary>Gets a new <see cref="T:System.Diagnostics.Process" /> component and associates it with the currently active process.</summary>
  1517.     /// <returns>A new <see cref="T:System.Diagnostics.Process" /> component associated with the process resource that is running the calling application.</returns>
  1518.     public static Process GetCurrentProcess()
  1519.     {
  1520.       return new Process(".", false, Microsoft.Win32.NativeMethods.GetCurrentProcessId(), (ProcessInfo) null);
  1521.     }
  1522.  
  1523.     /// <summary>Raises the <see cref="E:System.Diagnostics.Process.Exited" /> event.</summary>
  1524.     protected void OnExited()
  1525.     {
  1526.       EventHandler onExited = this.onExited;
  1527.       if (onExited == null)
  1528.         return;
  1529.       if (this.SynchronizingObject != null && this.SynchronizingObject.InvokeRequired)
  1530.         this.SynchronizingObject.BeginInvoke((Delegate) onExited, new object[2]
  1531.         {
  1532.           (object) this,
  1533.           (object) EventArgs.Empty
  1534.         });
  1535.       else
  1536.         onExited((object) this, EventArgs.Empty);
  1537.     }
  1538.  
  1539.     private SafeProcessHandle GetProcessHandle(int access, bool throwIfExited)
  1540.     {
  1541.       if (this.haveProcessHandle)
  1542.       {
  1543.         if (throwIfExited)
  1544.         {
  1545.           ProcessWaitHandle processWaitHandle = (ProcessWaitHandle) null;
  1546.           try
  1547.           {
  1548.             processWaitHandle = new ProcessWaitHandle(this.m_processHandle);
  1549.             if (processWaitHandle.WaitOne(0, false))
  1550.             {
  1551.               if (this.haveProcessId)
  1552.                 throw new InvalidOperationException(SR.GetString("ProcessHasExited", (object) this.processId.ToString((IFormatProvider) CultureInfo.CurrentCulture)));
  1553.               throw new InvalidOperationException(SR.GetString("ProcessHasExitedNoId"));
  1554.             }
  1555.           }
  1556.           finally
  1557.           {
  1558.             processWaitHandle?.Close();
  1559.           }
  1560.         }
  1561.         return this.m_processHandle;
  1562.       }
  1563.       this.EnsureState(Process.State.HaveId | Process.State.IsLocal);
  1564.       SafeProcessHandle invalidHandle = SafeProcessHandle.InvalidHandle;
  1565.       SafeProcessHandle processHandle = ProcessManager.OpenProcess(this.processId, access, throwIfExited);
  1566.       if (throwIfExited && (access & 1024) != 0 && (Microsoft.Win32.NativeMethods.GetExitCodeProcess(processHandle, out this.exitCode) && this.exitCode != 259))
  1567.         throw new InvalidOperationException(SR.GetString("ProcessHasExited", (object) this.processId.ToString((IFormatProvider) CultureInfo.CurrentCulture)));
  1568.       return processHandle;
  1569.     }
  1570.  
  1571.     private SafeProcessHandle GetProcessHandle(int access)
  1572.     {
  1573.       return this.GetProcessHandle(access, true);
  1574.     }
  1575.  
  1576.     private SafeProcessHandle OpenProcessHandle()
  1577.     {
  1578.       return this.OpenProcessHandle(2035711);
  1579.     }
  1580.  
  1581.     private SafeProcessHandle OpenProcessHandle(int access)
  1582.     {
  1583.       if (!this.haveProcessHandle)
  1584.       {
  1585.         if (this.disposed)
  1586.           throw new ObjectDisposedException(this.GetType().Name);
  1587.         this.SetProcessHandle(this.GetProcessHandle(access));
  1588.       }
  1589.       return this.m_processHandle;
  1590.     }
  1591.  
  1592.     private void RaiseOnExited()
  1593.     {
  1594.       if (this.raisedOnExited)
  1595.         return;
  1596.       lock (this)
  1597.       {
  1598.         if (this.raisedOnExited)
  1599.           return;
  1600.         this.raisedOnExited = true;
  1601.         this.OnExited();
  1602.       }
  1603.     }
  1604.  
  1605.     /// <summary>Discards any information about the associated process that has been cached inside the process component.</summary>
  1606.     public void Refresh()
  1607.     {
  1608.       this.processInfo = (ProcessInfo) null;
  1609.       this.threads = (ProcessThreadCollection) null;
  1610.       this.modules = (ProcessModuleCollection) null;
  1611.       this.mainWindowTitle = (string) null;
  1612.       this.exited = false;
  1613.       this.signaled = false;
  1614.       this.haveMainWindow = false;
  1615.       this.haveWorkingSetLimits = false;
  1616.       this.haveProcessorAffinity = false;
  1617.       this.havePriorityClass = false;
  1618.       this.haveExitTime = false;
  1619.       this.haveResponding = false;
  1620.       this.havePriorityBoostEnabled = false;
  1621.     }
  1622.  
  1623.     private void SetProcessHandle(SafeProcessHandle processHandle)
  1624.     {
  1625.       this.m_processHandle = processHandle;
  1626.       this.haveProcessHandle = true;
  1627.       if (!this.watchForExit)
  1628.         return;
  1629.       this.EnsureWatchingForExit();
  1630.     }
  1631.  
  1632.     private void SetProcessId(int processId)
  1633.     {
  1634.       this.processId = processId;
  1635.       this.haveProcessId = true;
  1636.     }
  1637.  
  1638.     private void SetWorkingSetLimits(object newMin, object newMax)
  1639.     {
  1640.       this.EnsureState(Process.State.IsNt);
  1641.       SafeProcessHandle handle = (SafeProcessHandle) null;
  1642.       try
  1643.       {
  1644.         handle = this.GetProcessHandle(1280);
  1645.         IntPtr min;
  1646.         IntPtr max;
  1647.         if (!Microsoft.Win32.NativeMethods.GetProcessWorkingSetSize(handle, out min, out max))
  1648.           throw new Win32Exception();
  1649.         if (newMin != null)
  1650.           min = (IntPtr) newMin;
  1651.         if (newMax != null)
  1652.           max = (IntPtr) newMax;
  1653.         if ((long) min > (long) max)
  1654.         {
  1655.           if (newMin != null)
  1656.             throw new ArgumentException(SR.GetString("BadMinWorkset"));
  1657.           throw new ArgumentException(SR.GetString("BadMaxWorkset"));
  1658.         }
  1659.         if (!Microsoft.Win32.NativeMethods.SetProcessWorkingSetSize(handle, min, max))
  1660.           throw new Win32Exception();
  1661.         if (!Microsoft.Win32.NativeMethods.GetProcessWorkingSetSize(handle, out min, out max))
  1662.           throw new Win32Exception();
  1663.         this.minWorkingSet = min;
  1664.         this.maxWorkingSet = max;
  1665.         this.haveWorkingSetLimits = true;
  1666.       }
  1667.       finally
  1668.       {
  1669.         this.ReleaseProcessHandle(handle);
  1670.       }
  1671.     }
  1672.  
  1673.     /// <summary>Starts (or reuses) the process resource that is specified by the <see cref="P:System.Diagnostics.Process.StartInfo" /> property of this <see cref="T:System.Diagnostics.Process" /> component and associates it with the component.</summary>
  1674.     /// <returns>
  1675.     /// <see langword="true" /> if a process resource is started; <see langword="false" /> if no new process resource is started (for example, if an existing process is reused).</returns>
  1676.     /// <exception cref="T:System.InvalidOperationException">No file name was specified in the <see cref="T:System.Diagnostics.Process" /> component's <see cref="P:System.Diagnostics.Process.StartInfo" />.-or- The <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> member of the <see cref="P:System.Diagnostics.Process.StartInfo" /> property is <see langword="true" /> while <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardInput" />, <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardOutput" />, or <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardError" /> is <see langword="true" />. </exception>
  1677.     /// <exception cref="T:System.ComponentModel.Win32Exception">There was an error in opening the associated file. </exception>
  1678.     /// <exception cref="T:System.ObjectDisposedException">The process object has already been disposed. </exception>
  1679.     public bool Start()
  1680.     {
  1681.       this.Close();
  1682.       ProcessStartInfo startInfo = this.StartInfo;
  1683.       if (startInfo.FileName.Length == 0)
  1684.         throw new InvalidOperationException(SR.GetString("FileNameMissing"));
  1685.       if (startInfo.UseShellExecute)
  1686.         return this.StartWithShellExecuteEx(startInfo);
  1687.       return this.StartWithCreateProcess(startInfo);
  1688.     }
  1689.  
  1690.     private static void CreatePipeWithSecurityAttributes(
  1691.       out SafeFileHandle hReadPipe,
  1692.       out SafeFileHandle hWritePipe,
  1693.       Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES lpPipeAttributes,
  1694.       int nSize)
  1695.     {
  1696.       if (!Microsoft.Win32.NativeMethods.CreatePipe(out hReadPipe, out hWritePipe, lpPipeAttributes, nSize) || hReadPipe.IsInvalid || hWritePipe.IsInvalid)
  1697.         throw new Win32Exception();
  1698.     }
  1699.  
  1700.     private void CreatePipe(
  1701.       out SafeFileHandle parentHandle,
  1702.       out SafeFileHandle childHandle,
  1703.       bool parentInputs)
  1704.     {
  1705.       Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES lpPipeAttributes = new Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES();
  1706.       lpPipeAttributes.bInheritHandle = true;
  1707.       SafeFileHandle safeFileHandle = (SafeFileHandle) null;
  1708.       try
  1709.       {
  1710.         if (parentInputs)
  1711.           Process.CreatePipeWithSecurityAttributes(out childHandle, out safeFileHandle, lpPipeAttributes, 0);
  1712.         else
  1713.           Process.CreatePipeWithSecurityAttributes(out safeFileHandle, out childHandle, lpPipeAttributes, 0);
  1714.         if (!Microsoft.Win32.NativeMethods.DuplicateHandle(new HandleRef((object) this, Microsoft.Win32.NativeMethods.GetCurrentProcess()), (System.Runtime.InteropServices.SafeHandle) safeFileHandle, new HandleRef((object) this, Microsoft.Win32.NativeMethods.GetCurrentProcess()), out parentHandle, 0, false, 2))
  1715.           throw new Win32Exception();
  1716.       }
  1717.       finally
  1718.       {
  1719.         if (safeFileHandle != null && !safeFileHandle.IsInvalid)
  1720.           safeFileHandle.Close();
  1721.       }
  1722.     }
  1723.  
  1724.     private static StringBuilder BuildCommandLine(
  1725.       string executableFileName,
  1726.       string arguments)
  1727.     {
  1728.       StringBuilder stringBuilder = new StringBuilder();
  1729.       string str = executableFileName.Trim();
  1730.       bool flag = str.StartsWith("\"", StringComparison.Ordinal) && str.EndsWith("\"", StringComparison.Ordinal);
  1731.       if (!flag)
  1732.         stringBuilder.Append("\"");
  1733.       stringBuilder.Append(str);
  1734.       if (!flag)
  1735.         stringBuilder.Append("\"");
  1736.       if (!string.IsNullOrEmpty(arguments))
  1737.       {
  1738.         stringBuilder.Append(" ");
  1739.         stringBuilder.Append(arguments);
  1740.       }
  1741.       return stringBuilder;
  1742.     }
  1743.  
  1744.     private bool StartWithCreateProcess(ProcessStartInfo startInfo)
  1745.     {
  1746.       if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput)
  1747.         throw new InvalidOperationException(SR.GetString("StandardOutputEncodingNotAllowed"));
  1748.       if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError)
  1749.         throw new InvalidOperationException(SR.GetString("StandardErrorEncodingNotAllowed"));
  1750.       if (this.disposed)
  1751.         throw new ObjectDisposedException(this.GetType().Name);
  1752.       StringBuilder stringBuilder = Process.BuildCommandLine(startInfo.FileName, startInfo.Arguments);
  1753.       Microsoft.Win32.NativeMethods.STARTUPINFO lpStartupInfo = new Microsoft.Win32.NativeMethods.STARTUPINFO();
  1754.       SafeNativeMethods.PROCESS_INFORMATION lpProcessInformation = new SafeNativeMethods.PROCESS_INFORMATION();
  1755.       SafeProcessHandle processHandle = new SafeProcessHandle();
  1756.       SafeThreadHandle safeThreadHandle = new SafeThreadHandle();
  1757.       int error = 0;
  1758.       SafeFileHandle parentHandle1 = (SafeFileHandle) null;
  1759.       SafeFileHandle parentHandle2 = (SafeFileHandle) null;
  1760.       SafeFileHandle parentHandle3 = (SafeFileHandle) null;
  1761.       GCHandle gcHandle = new GCHandle();
  1762.       lock (Process.s_CreateProcessLock)
  1763.       {
  1764.         try
  1765.         {
  1766.           if (startInfo.RedirectStandardInput || startInfo.RedirectStandardOutput || startInfo.RedirectStandardError)
  1767.           {
  1768.             if (startInfo.RedirectStandardInput)
  1769.               this.CreatePipe(out parentHandle1, out lpStartupInfo.hStdInput, true);
  1770.             else
  1771.               lpStartupInfo.hStdInput = new SafeFileHandle(Microsoft.Win32.NativeMethods.GetStdHandle(-10), false);
  1772.             if (startInfo.RedirectStandardOutput)
  1773.               this.CreatePipe(out parentHandle2, out lpStartupInfo.hStdOutput, false);
  1774.             else
  1775.               lpStartupInfo.hStdOutput = new SafeFileHandle(Microsoft.Win32.NativeMethods.GetStdHandle(-11), false);
  1776.             if (startInfo.RedirectStandardError)
  1777.               this.CreatePipe(out parentHandle3, out lpStartupInfo.hStdError, false);
  1778.             else
  1779.               lpStartupInfo.hStdError = new SafeFileHandle(Microsoft.Win32.NativeMethods.GetStdHandle(-12), false);
  1780.             lpStartupInfo.dwFlags = 256;
  1781.           }
  1782.           int num1 = 0;
  1783.           if (startInfo.CreateNoWindow)
  1784.             num1 |= 134217728;
  1785.           IntPtr num2 = (IntPtr) 0;
  1786.           if (startInfo.environmentVariables != null)
  1787.           {
  1788.             bool unicode = false;
  1789.             if (ProcessManager.IsNt)
  1790.             {
  1791.               num1 |= 1024;
  1792.               unicode = true;
  1793.             }
  1794.             gcHandle = GCHandle.Alloc((object) EnvironmentBlock.ToByteArray(startInfo.environmentVariables, unicode), GCHandleType.Pinned);
  1795.             num2 = gcHandle.AddrOfPinnedObject();
  1796.           }
  1797.           string lpCurrentDirectory = startInfo.WorkingDirectory;
  1798.           if (lpCurrentDirectory == string.Empty)
  1799.             lpCurrentDirectory = Environment.CurrentDirectory;
  1800.           if (startInfo.UserName.Length != 0)
  1801.           {
  1802.             if (startInfo.Password != null && startInfo.PasswordInClearText != null)
  1803.               throw new ArgumentException(SR.GetString("CantSetDuplicatePassword"));
  1804.             Microsoft.Win32.NativeMethods.LogonFlags logonFlags = (Microsoft.Win32.NativeMethods.LogonFlags) 0;
  1805.             if (startInfo.LoadUserProfile)
  1806.               logonFlags = Microsoft.Win32.NativeMethods.LogonFlags.LOGON_WITH_PROFILE;
  1807.             IntPtr num3 = IntPtr.Zero;
  1808.             try
  1809.             {
  1810.               num3 = startInfo.Password == null ? (startInfo.PasswordInClearText == null ? Marshal.StringToCoTaskMemUni(string.Empty) : Marshal.StringToCoTaskMemUni(startInfo.PasswordInClearText)) : Marshal.SecureStringToCoTaskMemUnicode(startInfo.Password);
  1811.               RuntimeHelpers.PrepareConstrainedRegions();
  1812.               bool processWithLogonW;
  1813.               try
  1814.               {
  1815.               }
  1816.               finally
  1817.               {
  1818.                 processWithLogonW = Microsoft.Win32.NativeMethods.CreateProcessWithLogonW(startInfo.UserName, startInfo.Domain, num3, logonFlags, (string) null, stringBuilder, num1, num2, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
  1819.                 if (!processWithLogonW)
  1820.                   error = Marshal.GetLastWin32Error();
  1821.                 if (lpProcessInformation.hProcess != (IntPtr) 0 && lpProcessInformation.hProcess != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
  1822.                   processHandle.InitialSetHandle(lpProcessInformation.hProcess);
  1823.                 if (lpProcessInformation.hThread != (IntPtr) 0 && lpProcessInformation.hThread != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
  1824.                   safeThreadHandle.InitialSetHandle(lpProcessInformation.hThread);
  1825.               }
  1826.               if (!processWithLogonW)
  1827.               {
  1828.                 if (error == 193 || error == 216)
  1829.                   throw new Win32Exception(error, SR.GetString("InvalidApplication"));
  1830.                 throw new Win32Exception(error);
  1831.               }
  1832.             }
  1833.             finally
  1834.             {
  1835.               if (num3 != IntPtr.Zero)
  1836.                 Marshal.ZeroFreeCoTaskMemUnicode(num3);
  1837.             }
  1838.           }
  1839.           else
  1840.           {
  1841.             RuntimeHelpers.PrepareConstrainedRegions();
  1842.             bool process;
  1843.             try
  1844.             {
  1845.             }
  1846.             finally
  1847.             {
  1848.               process = Microsoft.Win32.NativeMethods.CreateProcess((string) null, stringBuilder, (Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES) null, (Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES) null, true, num1, num2, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
  1849.               if (!process)
  1850.                 error = Marshal.GetLastWin32Error();
  1851.               if (lpProcessInformation.hProcess != (IntPtr) 0 && lpProcessInformation.hProcess != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
  1852.                 processHandle.InitialSetHandle(lpProcessInformation.hProcess);
  1853.               if (lpProcessInformation.hThread != (IntPtr) 0 && lpProcessInformation.hThread != Microsoft.Win32.NativeMethods.INVALID_HANDLE_VALUE)
  1854.                 safeThreadHandle.InitialSetHandle(lpProcessInformation.hThread);
  1855.             }
  1856.             if (!process)
  1857.             {
  1858.               if (error == 193 || error == 216)
  1859.                 throw new Win32Exception(error, SR.GetString("InvalidApplication"));
  1860.               throw new Win32Exception(error);
  1861.             }
  1862.           }
  1863.         }
  1864.         finally
  1865.         {
  1866.           if (gcHandle.IsAllocated)
  1867.             gcHandle.Free();
  1868.           lpStartupInfo.Dispose();
  1869.         }
  1870.       }
  1871.       if (startInfo.RedirectStandardInput)
  1872.       {
  1873.         this.standardInput = new StreamWriter((Stream) new FileStream(parentHandle1, FileAccess.Write, 4096, false), Console.InputEncoding, 4096);
  1874.         this.standardInput.AutoFlush = true;
  1875.       }
  1876.       if (startInfo.RedirectStandardOutput)
  1877.       {
  1878.         Encoding encoding = startInfo.StandardOutputEncoding != null ? startInfo.StandardOutputEncoding : Console.OutputEncoding;
  1879.         this.standardOutput = new StreamReader((Stream) new FileStream(parentHandle2, FileAccess.Read, 4096, false), encoding, true, 4096);
  1880.       }
  1881.       if (startInfo.RedirectStandardError)
  1882.       {
  1883.         Encoding encoding = startInfo.StandardErrorEncoding != null ? startInfo.StandardErrorEncoding : Console.OutputEncoding;
  1884.         this.standardError = new StreamReader((Stream) new FileStream(parentHandle3, FileAccess.Read, 4096, false), encoding, true, 4096);
  1885.       }
  1886.       bool flag = false;
  1887.       if (!processHandle.IsInvalid)
  1888.       {
  1889.         this.SetProcessHandle(processHandle);
  1890.         this.SetProcessId(lpProcessInformation.dwProcessId);
  1891.         safeThreadHandle.Close();
  1892.         flag = true;
  1893.       }
  1894.       return flag;
  1895.     }
  1896.  
  1897.     private bool StartWithShellExecuteEx(ProcessStartInfo startInfo)
  1898.     {
  1899.       if (this.disposed)
  1900.         throw new ObjectDisposedException(this.GetType().Name);
  1901.       if (!string.IsNullOrEmpty(startInfo.UserName) || startInfo.Password != null)
  1902.         throw new InvalidOperationException(SR.GetString("CantStartAsUser"));
  1903.       if (startInfo.RedirectStandardInput || startInfo.RedirectStandardOutput || startInfo.RedirectStandardError)
  1904.         throw new InvalidOperationException(SR.GetString("CantRedirectStreams"));
  1905.       if (startInfo.StandardErrorEncoding != null)
  1906.         throw new InvalidOperationException(SR.GetString("StandardErrorEncodingNotAllowed"));
  1907.       if (startInfo.StandardOutputEncoding != null)
  1908.         throw new InvalidOperationException(SR.GetString("StandardOutputEncodingNotAllowed"));
  1909.       if (startInfo.environmentVariables != null)
  1910.         throw new InvalidOperationException(SR.GetString("CantUseEnvVars"));
  1911.       Microsoft.Win32.NativeMethods.ShellExecuteInfo executeInfo = new Microsoft.Win32.NativeMethods.ShellExecuteInfo();
  1912.       executeInfo.fMask = 64;
  1913.       if (startInfo.ErrorDialog)
  1914.         executeInfo.hwnd = startInfo.ErrorDialogParentHandle;
  1915.       else
  1916.         executeInfo.fMask |= 1024;
  1917.       switch (startInfo.WindowStyle)
  1918.       {
  1919.         case ProcessWindowStyle.Hidden:
  1920.           executeInfo.nShow = 0;
  1921.           break;
  1922.         case ProcessWindowStyle.Minimized:
  1923.           executeInfo.nShow = 2;
  1924.           break;
  1925.         case ProcessWindowStyle.Maximized:
  1926.           executeInfo.nShow = 3;
  1927.           break;
  1928.         default:
  1929.           executeInfo.nShow = 1;
  1930.           break;
  1931.       }
  1932.       try
  1933.       {
  1934.         if (startInfo.FileName.Length != 0)
  1935.           executeInfo.lpFile = Marshal.StringToHGlobalAuto(startInfo.FileName);
  1936.         if (startInfo.Verb.Length != 0)
  1937.           executeInfo.lpVerb = Marshal.StringToHGlobalAuto(startInfo.Verb);
  1938.         if (startInfo.Arguments.Length != 0)
  1939.           executeInfo.lpParameters = Marshal.StringToHGlobalAuto(startInfo.Arguments);
  1940.         if (startInfo.WorkingDirectory.Length != 0)
  1941.           executeInfo.lpDirectory = Marshal.StringToHGlobalAuto(startInfo.WorkingDirectory);
  1942.         executeInfo.fMask |= 256;
  1943.         ShellExecuteHelper shellExecuteHelper = new ShellExecuteHelper(executeInfo);
  1944.         if (!shellExecuteHelper.ShellExecuteOnSTAThread())
  1945.         {
  1946.           int error = shellExecuteHelper.ErrorCode;
  1947.           if (error == 0)
  1948.           {
  1949.             long hInstApp = (long) executeInfo.hInstApp;
  1950.             switch (hInstApp - 2L)
  1951.             {
  1952.               case 0:
  1953.                 error = 2;
  1954.                 break;
  1955.               case 1:
  1956.                 error = 3;
  1957.                 break;
  1958.               case 2:
  1959.               case 4:
  1960.               case 5:
  1961. label_41:
  1962.                 error = (int) executeInfo.hInstApp;
  1963.                 break;
  1964.               case 3:
  1965.                 error = 5;
  1966.                 break;
  1967.               case 6:
  1968.                 error = 8;
  1969.                 break;
  1970.               default:
  1971.                 switch (hInstApp - 26L)
  1972.                 {
  1973.                   case 0:
  1974.                     error = 32;
  1975.                     break;
  1976.                   case 2:
  1977.                   case 3:
  1978.                   case 4:
  1979.                     error = 1156;
  1980.                     break;
  1981.                   case 5:
  1982.                     error = 1155;
  1983.                     break;
  1984.                   case 6:
  1985.                     error = 1157;
  1986.                     break;
  1987.                   default:
  1988.                     goto label_41;
  1989.                 }
  1990.             }
  1991.           }
  1992.           if (error == 193 || error == 216)
  1993.             throw new Win32Exception(error, SR.GetString("InvalidApplication"));
  1994.           throw new Win32Exception(error);
  1995.         }
  1996.       }
  1997.       finally
  1998.       {
  1999.         if (executeInfo.lpFile != (IntPtr) 0)
  2000.           Marshal.FreeHGlobal(executeInfo.lpFile);
  2001.         if (executeInfo.lpVerb != (IntPtr) 0)
  2002.           Marshal.FreeHGlobal(executeInfo.lpVerb);
  2003.         if (executeInfo.lpParameters != (IntPtr) 0)
  2004.           Marshal.FreeHGlobal(executeInfo.lpParameters);
  2005.         if (executeInfo.lpDirectory != (IntPtr) 0)
  2006.           Marshal.FreeHGlobal(executeInfo.lpDirectory);
  2007.       }
  2008.       if (!(executeInfo.hProcess != (IntPtr) 0))
  2009.         return false;
  2010.       this.SetProcessHandle(new SafeProcessHandle(executeInfo.hProcess));
  2011.       return true;
  2012.     }
  2013.  
  2014.     /// <summary>Starts a process resource by specifying the name of an application, a user name, a password, and a domain and associates the resource with a new <see cref="T:System.Diagnostics.Process" /> component.</summary>
  2015.     /// <param name="fileName">The name of an application file to run in the process.</param>
  2016.     /// <param name="userName">The user name to use when starting the process.</param>
  2017.     /// <param name="password">A <see cref="T:System.Security.SecureString" /> that contains the password to use when starting the process.</param>
  2018.     /// <param name="domain">The domain to use when starting the process.</param>
  2019.     /// <returns>A new <see cref="T:System.Diagnostics.Process" /> that is associated with the process resource, or <see langword="null" /> if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its <see cref="P:System.Diagnostics.Process.HasExited" /> property already set to <see langword="true" />. In this case, the started process may have activated an existing instance of itself and then exited.</returns>
  2020.     /// <exception cref="T:System.InvalidOperationException">No file name was specified. </exception>
  2021.     /// <exception cref="T:System.ComponentModel.Win32Exception">
  2022.     /// <paramref name="fileName" /> is not an executable (.exe) file.</exception>
  2023.     /// <exception cref="T:System.ComponentModel.Win32Exception">There was an error in opening the associated file. </exception>
  2024.     /// <exception cref="T:System.ObjectDisposedException">The process object has already been disposed. </exception>
  2025.     public static Process Start(
  2026.       string fileName,
  2027.       string userName,
  2028.       SecureString password,
  2029.       string domain)
  2030.     {
  2031.       return Process.Start(new ProcessStartInfo(fileName)
  2032.       {
  2033.         UserName = userName,
  2034.         Password = password,
  2035.         Domain = domain,
  2036.         UseShellExecute = false
  2037.       });
  2038.     }
  2039.  
  2040.     /// <summary>Starts a process resource by specifying the name of an application, a set of command-line arguments, a user name, a password, and a domain and associates the resource with a new <see cref="T:System.Diagnostics.Process" /> component.</summary>
  2041.     /// <param name="fileName">The name of an application file to run in the process. </param>
  2042.     /// <param name="arguments">Command-line arguments to pass when starting the process. </param>
  2043.     /// <param name="userName">The user name to use when starting the process.</param>
  2044.     /// <param name="password">A <see cref="T:System.Security.SecureString" /> that contains the password to use when starting the process.</param>
  2045.     /// <param name="domain">The domain to use when starting the process.</param>
  2046.     /// <returns>A new <see cref="T:System.Diagnostics.Process" /> that is associated with the process resource, or <see langword="null" /> if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its <see cref="P:System.Diagnostics.Process.HasExited" /> property already set to <see langword="true" />. In this case, the started process may have activated an existing instance of itself and then exited.</returns>
  2047.     /// <exception cref="T:System.InvalidOperationException">No file name was specified.</exception>
  2048.     /// <exception cref="T:System.ComponentModel.Win32Exception">
  2049.     /// <paramref name="fileName" /> is not an executable (.exe) file.</exception>
  2050.     /// <exception cref="T:System.ComponentModel.Win32Exception">An error occurred when opening the associated file. -or-The sum of the length of the arguments and the length of the full path to the associated file exceeds 2080. The error message associated with this exception can be one of the following: "The data area passed to a system call is too small." or "Access is denied."</exception>
  2051.     /// <exception cref="T:System.ObjectDisposedException">The process object has already been disposed. </exception>
  2052.     public static Process Start(
  2053.       string fileName,
  2054.       string arguments,
  2055.       string userName,
  2056.       SecureString password,
  2057.       string domain)
  2058.     {
  2059.       return Process.Start(new ProcessStartInfo(fileName, arguments)
  2060.       {
  2061.         UserName = userName,
  2062.         Password = password,
  2063.         Domain = domain,
  2064.         UseShellExecute = false
  2065.       });
  2066.     }
  2067.  
  2068.     /// <summary>Starts a process resource by specifying the name of a document or application file and associates the resource with a new <see cref="T:System.Diagnostics.Process" /> component.</summary>
  2069.     /// <param name="fileName">The name of a document or application file to run in the process. </param>
  2070.     /// <returns>A new <see cref="T:System.Diagnostics.Process" /> that is associated with the process resource, or <see langword="null" /> if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its <see cref="P:System.Diagnostics.Process.HasExited" /> property already set to <see langword="true" />. In this case, the started process may have activated an existing instance of itself and then exited.</returns>
  2071.     /// <exception cref="T:System.ComponentModel.Win32Exception">An error occurred when opening the associated file. </exception>
  2072.     /// <exception cref="T:System.ObjectDisposedException">The process object has already been disposed. </exception>
  2073.     /// <exception cref="T:System.IO.FileNotFoundException">The PATH environment variable has a string containing quotes.</exception>
  2074.     public static Process Start(string fileName)
  2075.     {
  2076.       return Process.Start(new ProcessStartInfo(fileName));
  2077.     }
  2078.  
  2079.     /// <summary>Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new <see cref="T:System.Diagnostics.Process" /> component.</summary>
  2080.     /// <param name="fileName">The name of an application file to run in the process. </param>
  2081.     /// <param name="arguments">Command-line arguments to pass when starting the process. </param>
  2082.     /// <returns>A new <see cref="T:System.Diagnostics.Process" /> that is associated with the process resource, or <see langword="null" /> if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its <see cref="P:System.Diagnostics.Process.HasExited" /> property already set to <see langword="true" />. In this case, the started process may have activated an existing instance of itself and then exited.</returns>
  2083.     /// <exception cref="T:System.InvalidOperationException">The <paramref name="fileName" /> or <paramref name="arguments" /> parameter is <see langword="null" />. </exception>
  2084.     /// <exception cref="T:System.ComponentModel.Win32Exception">An error occurred when opening the associated file. -or-The sum of the length of the arguments and the length of the full path to the process exceeds 2080. The error message associated with this exception can be one of the following: "The data area passed to a system call is too small." or "Access is denied."</exception>
  2085.     /// <exception cref="T:System.ObjectDisposedException">The process object has already been disposed. </exception>
  2086.     /// <exception cref="T:System.IO.FileNotFoundException">The PATH environment variable has a string containing quotes.</exception>
  2087.     public static Process Start(string fileName, string arguments)
  2088.     {
  2089.       return Process.Start(new ProcessStartInfo(fileName, arguments));
  2090.     }
  2091.  
  2092.     /// <summary>Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new <see cref="T:System.Diagnostics.Process" /> component.</summary>
  2093.     /// <param name="startInfo">The <see cref="T:System.Diagnostics.ProcessStartInfo" /> that contains the information that is used to start the process, including the file name and any command-line arguments. </param>
  2094.     /// <returns>A new <see cref="T:System.Diagnostics.Process" /> that is associated with the process resource, or <see langword="null" /> if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its <see cref="P:System.Diagnostics.Process.HasExited" /> property already set to <see langword="true" />. In this case, the started process may have activated an existing instance of itself and then exited.</returns>
  2095.     /// <exception cref="T:System.InvalidOperationException">No file name was specified in the <paramref name="startInfo" /> parameter's <see cref="P:System.Diagnostics.ProcessStartInfo.FileName" /> property.-or- The <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> property of the <paramref name="startInfo" /> parameter is <see langword="true" /> and the <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardInput" />, <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardOutput" />, or <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardError" /> property is also <see langword="true" />.-or-The <see cref="P:System.Diagnostics.ProcessStartInfo.UseShellExecute" /> property of the <paramref name="startInfo" /> parameter is <see langword="true" /> and the <see cref="P:System.Diagnostics.ProcessStartInfo.UserName" /> property is not <see langword="null" /> or empty or the <see cref="P:System.Diagnostics.ProcessStartInfo.Password" /> property is not <see langword="null" />.</exception>
  2096.     /// <exception cref="T:System.ArgumentNullException">The <paramref name="startInfo" /> parameter is <see langword="null" />. </exception>
  2097.     /// <exception cref="T:System.ObjectDisposedException">The process object has already been disposed. </exception>
  2098.     /// <exception cref="T:System.IO.FileNotFoundException">The file specified in the <paramref name="startInfo" /> parameter's <see cref="P:System.Diagnostics.ProcessStartInfo.FileName" /> property could not be found.</exception>
  2099.     /// <exception cref="T:System.ComponentModel.Win32Exception">An error occurred when opening the associated file. -or-The sum of the length of the arguments and the length of the full path to the process exceeds 2080. The error message associated with this exception can be one of the following: "The data area passed to a system call is too small." or "Access is denied."</exception>
  2100.     public static Process Start(ProcessStartInfo startInfo)
  2101.     {
  2102.       Process process = new Process();
  2103.       if (startInfo == null)
  2104.         throw new ArgumentNullException(nameof (startInfo));
  2105.       process.StartInfo = startInfo;
  2106.       if (process.Start())
  2107.         return process;
  2108.       return (Process) null;
  2109.     }
  2110.  
  2111.     /// <summary>Immediately stops the associated process.</summary>
  2112.     /// <exception cref="T:System.ComponentModel.Win32Exception">The associated process could not be terminated. -or-The process is terminating.-or- The associated process is a Win16 executable.</exception>
  2113.     /// <exception cref="T:System.NotSupportedException">You are attempting to call <see cref="M:System.Diagnostics.Process.Kill" /> for a process that is running on a remote computer. The method is available only for processes running on the local computer.</exception>
  2114.     /// <exception cref="T:System.InvalidOperationException">The process has already exited. -or-There is no process associated with this <see cref="T:System.Diagnostics.Process" /> object.</exception>
  2115.     public void Kill()
  2116.     {
  2117.       SafeProcessHandle safeProcessHandle = (SafeProcessHandle) null;
  2118.       try
  2119.       {
  2120.         safeProcessHandle = this.GetProcessHandle(1);
  2121.         if (!Microsoft.Win32.NativeMethods.TerminateProcess(safeProcessHandle, -1))
  2122.           throw new Win32Exception();
  2123.       }
  2124.       finally
  2125.       {
  2126.         this.ReleaseProcessHandle(safeProcessHandle);
  2127.       }
  2128.     }
  2129.  
  2130.     private void StopWatchingForExit()
  2131.     {
  2132.       if (!this.watchingForExit)
  2133.         return;
  2134.       lock (this)
  2135.       {
  2136.         if (!this.watchingForExit)
  2137.           return;
  2138.         this.watchingForExit = false;
  2139.         this.registeredWaitHandle.Unregister((WaitHandle) null);
  2140.         this.waitHandle.Close();
  2141.         this.waitHandle = (WaitHandle) null;
  2142.         this.registeredWaitHandle = (RegisteredWaitHandle) null;
  2143.       }
  2144.     }
  2145.  
  2146.     /// <summary>Formats the process's name as a string, combined with the parent component type, if applicable.</summary>
  2147.     /// <returns>The <see cref="P:System.Diagnostics.Process.ProcessName" />, combined with the base component's <see cref="M:System.Object.ToString" /> return value.</returns>
  2148.     /// <exception cref="T:System.PlatformNotSupportedException">
  2149.     /// <see cref="M:System.Diagnostics.Process.ToString" /> is not supported on Windows 98.</exception>
  2150.     public override string ToString()
  2151.     {
  2152.       if (!this.Associated)
  2153.         return base.ToString();
  2154.       string str = string.Empty;
  2155.       try
  2156.       {
  2157.         str = this.ProcessName;
  2158.       }
  2159.       catch (PlatformNotSupportedException ex)
  2160.       {
  2161.       }
  2162.       if (str.Length == 0)
  2163.         return base.ToString();
  2164.       return string.Format((IFormatProvider) CultureInfo.CurrentCulture, "{0} ({1})", new object[2]
  2165.       {
  2166.         (object) base.ToString(),
  2167.         (object) str
  2168.       });
  2169.     }
  2170.  
  2171.     /// <summary>Instructs the <see cref="T:System.Diagnostics.Process" /> component to wait the specified number of milliseconds for the associated process to exit.</summary>
  2172.     /// <param name="milliseconds">The amount of time, in milliseconds, to wait for the associated process to exit. The maximum is the largest possible value of a 32-bit integer, which represents infinity to the operating system. </param>
  2173.     /// <returns>
  2174.     /// <see langword="true" /> if the associated process has exited; otherwise, <see langword="false" />.</returns>
  2175.     /// <exception cref="T:System.ComponentModel.Win32Exception">The wait setting could not be accessed. </exception>
  2176.     /// <exception cref="T:System.SystemException">No process <see cref="P:System.Diagnostics.Process.Id" /> has been set, and a <see cref="P:System.Diagnostics.Process.Handle" /> from which the <see cref="P:System.Diagnostics.Process.Id" /> property can be determined does not exist.-or- There is no process associated with this <see cref="T:System.Diagnostics.Process" /> object.-or- You are attempting to call <see cref="M:System.Diagnostics.Process.WaitForExit(System.Int32)" /> for a process that is running on a remote computer. This method is available only for processes that are running on the local computer. </exception>
  2177.     public bool WaitForExit(int milliseconds)
  2178.     {
  2179.       SafeProcessHandle safeProcessHandle = (SafeProcessHandle) null;
  2180.       ProcessWaitHandle processWaitHandle = (ProcessWaitHandle) null;
  2181.       bool flag;
  2182.       try
  2183.       {
  2184.         safeProcessHandle = this.GetProcessHandle(1048576, false);
  2185.         if (safeProcessHandle.IsInvalid)
  2186.         {
  2187.           flag = true;
  2188.         }
  2189.         else
  2190.         {
  2191.           processWaitHandle = new ProcessWaitHandle(safeProcessHandle);
  2192.           if (processWaitHandle.WaitOne(milliseconds, false))
  2193.           {
  2194.             flag = true;
  2195.             this.signaled = true;
  2196.           }
  2197.           else
  2198.           {
  2199.             flag = false;
  2200.             this.signaled = false;
  2201.           }
  2202.         }
  2203.       }
  2204.       finally
  2205.       {
  2206.         processWaitHandle?.Close();
  2207.         if (this.output != null && milliseconds == -1)
  2208.           this.output.WaitUtilEOF();
  2209.         if (this.error != null && milliseconds == -1)
  2210.           this.error.WaitUtilEOF();
  2211.         this.ReleaseProcessHandle(safeProcessHandle);
  2212.       }
  2213.       if (flag && this.watchForExit)
  2214.         this.RaiseOnExited();
  2215.       return flag;
  2216.     }
  2217.  
  2218.     /// <summary>Instructs the <see cref="T:System.Diagnostics.Process" /> component to wait indefinitely for the associated process to exit.</summary>
  2219.     /// <exception cref="T:System.ComponentModel.Win32Exception">The wait setting could not be accessed. </exception>
  2220.     /// <exception cref="T:System.SystemException">No process <see cref="P:System.Diagnostics.Process.Id" /> has been set, and a <see cref="P:System.Diagnostics.Process.Handle" /> from which the <see cref="P:System.Diagnostics.Process.Id" /> property can be determined does not exist.-or- There is no process associated with this <see cref="T:System.Diagnostics.Process" /> object.-or- You are attempting to call <see cref="M:System.Diagnostics.Process.WaitForExit" /> for a process that is running on a remote computer. This method is available only for processes that are running on the local computer. </exception>
  2221.     public void WaitForExit()
  2222.     {
  2223.       this.WaitForExit(-1);
  2224.     }
  2225.  
  2226.     /// <summary>Causes the <see cref="T:System.Diagnostics.Process" /> component to wait the specified number of milliseconds for the associated process to enter an idle state. This overload applies only to processes with a user interface and, therefore, a message loop.</summary>
  2227.     /// <param name="milliseconds">A value of 1 to <see cref="F:System.Int32.MaxValue" /> that specifies the amount of time, in milliseconds, to wait for the associated process to become idle. A value of 0 specifies an immediate return, and a value of -1 specifies an infinite wait. </param>
  2228.     /// <returns>
  2229.     /// <see langword="true" /> if the associated process has reached an idle state; otherwise, <see langword="false" />.</returns>
  2230.     /// <exception cref="T:System.InvalidOperationException">The process does not have a graphical interface.-or-An unknown error occurred. The process failed to enter an idle state.-or-The process has already exited. -or-No process is associated with this <see cref="T:System.Diagnostics.Process" /> object.</exception>
  2231.     public bool WaitForInputIdle(int milliseconds)
  2232.     {
  2233.       SafeProcessHandle handle = (SafeProcessHandle) null;
  2234.       try
  2235.       {
  2236.         handle = this.GetProcessHandle(1049600);
  2237.         switch (Microsoft.Win32.NativeMethods.WaitForInputIdle(handle, milliseconds))
  2238.         {
  2239.           case 0:
  2240.             return true;
  2241.           case 258:
  2242.             return false;
  2243.           default:
  2244.             throw new InvalidOperationException(SR.GetString("InputIdleUnkownError"));
  2245.         }
  2246.       }
  2247.       finally
  2248.       {
  2249.         this.ReleaseProcessHandle(handle);
  2250.       }
  2251.     }
  2252.  
  2253.     /// <summary>Causes the <see cref="T:System.Diagnostics.Process" /> component to wait indefinitely for the associated process to enter an idle state. This overload applies only to processes with a user interface and, therefore, a message loop.</summary>
  2254.     /// <returns>
  2255.     /// <see langword="true" /> if the associated process has reached an idle state.</returns>
  2256.     /// <exception cref="T:System.InvalidOperationException">The process does not have a graphical interface.-or-An unknown error occurred. The process failed to enter an idle state.-or-The process has already exited. -or-No process is associated with this <see cref="T:System.Diagnostics.Process" /> object.</exception>
  2257.     public bool WaitForInputIdle()
  2258.     {
  2259.       return this.WaitForInputIdle(int.MaxValue);
  2260.     }
  2261.  
  2262.     /// <summary>Begins asynchronous read operations on the redirected <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream of the application.</summary>
  2263.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardOutput" /> property is <see langword="false" />.- or - An asynchronous read operation is already in progress on the <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream.- or - The <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream has been used by a synchronous read operation. </exception>
  2264.     [ComVisible(false)]
  2265.     public void BeginOutputReadLine()
  2266.     {
  2267.       if (this.outputStreamReadMode == Process.StreamReadMode.undefined)
  2268.         this.outputStreamReadMode = Process.StreamReadMode.asyncMode;
  2269.       else if (this.outputStreamReadMode != Process.StreamReadMode.asyncMode)
  2270.         throw new InvalidOperationException(SR.GetString("CantMixSyncAsyncOperation"));
  2271.       if (this.pendingOutputRead)
  2272.         throw new InvalidOperationException(SR.GetString("PendingAsyncOperation"));
  2273.       this.pendingOutputRead = true;
  2274.       if (this.output == null)
  2275.       {
  2276.         if (this.standardOutput == null)
  2277.           throw new InvalidOperationException(SR.GetString("CantGetStandardOut"));
  2278.         this.output = new AsyncStreamReader(this, this.standardOutput.BaseStream, new UserCallBack(this.OutputReadNotifyUser), this.standardOutput.CurrentEncoding);
  2279.       }
  2280.       this.output.BeginReadLine();
  2281.     }
  2282.  
  2283.     /// <summary>Begins asynchronous read operations on the redirected <see cref="P:System.Diagnostics.Process.StandardError" /> stream of the application.</summary>
  2284.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.ProcessStartInfo.RedirectStandardError" /> property is <see langword="false" />.- or - An asynchronous read operation is already in progress on the <see cref="P:System.Diagnostics.Process.StandardError" /> stream.- or - The <see cref="P:System.Diagnostics.Process.StandardError" /> stream has been used by a synchronous read operation. </exception>
  2285.     [ComVisible(false)]
  2286.     public void BeginErrorReadLine()
  2287.     {
  2288.       if (this.errorStreamReadMode == Process.StreamReadMode.undefined)
  2289.         this.errorStreamReadMode = Process.StreamReadMode.asyncMode;
  2290.       else if (this.errorStreamReadMode != Process.StreamReadMode.asyncMode)
  2291.         throw new InvalidOperationException(SR.GetString("CantMixSyncAsyncOperation"));
  2292.       if (this.pendingErrorRead)
  2293.         throw new InvalidOperationException(SR.GetString("PendingAsyncOperation"));
  2294.       this.pendingErrorRead = true;
  2295.       if (this.error == null)
  2296.       {
  2297.         if (this.standardError == null)
  2298.           throw new InvalidOperationException(SR.GetString("CantGetStandardError"));
  2299.         this.error = new AsyncStreamReader(this, this.standardError.BaseStream, new UserCallBack(this.ErrorReadNotifyUser), this.standardError.CurrentEncoding);
  2300.       }
  2301.       this.error.BeginReadLine();
  2302.     }
  2303.  
  2304.     /// <summary>Cancels the asynchronous read operation on the redirected <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream of an application.</summary>
  2305.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.Process.StandardOutput" /> stream is not enabled for asynchronous read operations. </exception>
  2306.     [ComVisible(false)]
  2307.     public void CancelOutputRead()
  2308.     {
  2309.       if (this.output == null)
  2310.         throw new InvalidOperationException(SR.GetString("NoAsyncOperation"));
  2311.       this.output.CancelOperation();
  2312.       this.pendingOutputRead = false;
  2313.     }
  2314.  
  2315.     /// <summary>Cancels the asynchronous read operation on the redirected <see cref="P:System.Diagnostics.Process.StandardError" /> stream of an application.</summary>
  2316.     /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Diagnostics.Process.StandardError" /> stream is not enabled for asynchronous read operations. </exception>
  2317.     [ComVisible(false)]
  2318.     public void CancelErrorRead()
  2319.     {
  2320.       if (this.error == null)
  2321.         throw new InvalidOperationException(SR.GetString("NoAsyncOperation"));
  2322.       this.error.CancelOperation();
  2323.       this.pendingErrorRead = false;
  2324.     }
  2325.  
  2326.     internal void OutputReadNotifyUser(string data)
  2327.     {
  2328.       DataReceivedEventHandler outputDataReceived = this.OutputDataReceived;
  2329.       if (outputDataReceived == null)
  2330.         return;
  2331.       DataReceivedEventArgs e = new DataReceivedEventArgs(data);
  2332.       if (this.SynchronizingObject != null && this.SynchronizingObject.InvokeRequired)
  2333.         this.SynchronizingObject.Invoke((Delegate) outputDataReceived, new object[2]
  2334.         {
  2335.           (object) this,
  2336.           (object) e
  2337.         });
  2338.       else
  2339.         outputDataReceived((object) this, e);
  2340.     }
  2341.  
  2342.     internal void ErrorReadNotifyUser(string data)
  2343.     {
  2344.       DataReceivedEventHandler errorDataReceived = this.ErrorDataReceived;
  2345.       if (errorDataReceived == null)
  2346.         return;
  2347.       DataReceivedEventArgs e = new DataReceivedEventArgs(data);
  2348.       if (this.SynchronizingObject != null && this.SynchronizingObject.InvokeRequired)
  2349.         this.SynchronizingObject.Invoke((Delegate) errorDataReceived, new object[2]
  2350.         {
  2351.           (object) this,
  2352.           (object) e
  2353.         });
  2354.       else
  2355.         errorDataReceived((object) this, e);
  2356.     }
  2357.  
  2358.     private enum StreamReadMode
  2359.     {
  2360.       undefined,
  2361.       syncMode,
  2362.       asyncMode,
  2363.     }
  2364.  
  2365.     private enum State
  2366.     {
  2367.       HaveId = 1,
  2368.       IsLocal = 2,
  2369.       IsNt = 4,
  2370.       HaveProcessInfo = 8,
  2371.       HaveNtProcessInfo = 12, // 0x0000000C
  2372.       Exited = 16, // 0x00000010
  2373.       Associated = 32, // 0x00000020
  2374.       IsWin2k = 64, // 0x00000040
  2375.     }
  2376.   }
  2377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement