mscorlib(4.0.0.0) API with additions
Console.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
3 using System.IO;
7 using System.Security;
9 using System.Text;
10 using System.Threading;
11 
12 namespace System
13 {
15  public static class Console
16  {
17  [Flags]
18  internal enum ControlKeyState
19  {
20  RightAltPressed = 0x1,
21  LeftAltPressed = 0x2,
22  RightCtrlPressed = 0x4,
23  LeftCtrlPressed = 0x8,
24  ShiftPressed = 0x10,
25  NumLockOn = 0x20,
26  ScrollLockOn = 0x40,
27  CapsLockOn = 0x80,
28  EnhancedKey = 0x100
29  }
30 
31  internal sealed class ControlCHooker : CriticalFinalizerObject
32  {
33  private bool _hooked;
34 
35  [SecurityCritical]
36  private Win32Native.ConsoleCtrlHandlerRoutine _handler;
37 
38  [SecurityCritical]
39  internal ControlCHooker()
40  {
41  _handler = BreakEvent;
42  }
43 
44  ~ControlCHooker()
45  {
46  Unhook();
47  }
48 
49  [SecuritySafeCritical]
50  internal void Hook()
51  {
52  if (!_hooked)
53  {
54  if (!Win32Native.SetConsoleCtrlHandler(_handler, addOrRemove: true))
55  {
56  __Error.WinIOError();
57  }
58  _hooked = true;
59  }
60  }
61 
62  [SecuritySafeCritical]
63  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
64  internal void Unhook()
65  {
66  if (_hooked)
67  {
68  if (!Win32Native.SetConsoleCtrlHandler(_handler, addOrRemove: false))
69  {
70  __Error.WinIOError();
71  }
72  _hooked = false;
73  }
74  }
75  }
76 
77  private sealed class ControlCDelegateData
78  {
79  internal ConsoleSpecialKey ControlKey;
80 
81  internal bool Cancel;
82 
83  internal bool DelegateStarted;
84 
85  internal ManualResetEvent CompletionEvent;
86 
87  internal ConsoleCancelEventHandler CancelCallbacks;
88 
89  internal ControlCDelegateData(ConsoleSpecialKey controlKey, ConsoleCancelEventHandler cancelCallbacks)
90  {
91  ControlKey = controlKey;
92  CancelCallbacks = cancelCallbacks;
93  CompletionEvent = new ManualResetEvent(initialState: false);
94  }
95  }
96 
97  private const int DefaultConsoleBufferSize = 256;
98 
99  private const short AltVKCode = 18;
100 
101  private const int NumberLockVKCode = 144;
102 
103  private const int CapsLockVKCode = 20;
104 
105  private const int MinBeepFrequency = 37;
106 
107  private const int MaxBeepFrequency = 32767;
108 
109  private const int MaxConsoleTitleLength = 24500;
110 
111  private static readonly UnicodeEncoding StdConUnicodeEncoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: false);
112 
113  private static volatile TextReader _in;
114 
115  private static volatile TextWriter _out;
116 
117  private static volatile TextWriter _error;
118 
119  private static volatile ConsoleCancelEventHandler _cancelCallbacks;
120 
121  private static volatile ControlCHooker _hooker;
122 
123  [SecurityCritical]
124  private static Win32Native.InputRecord _cachedInputRecord;
125 
126  private static volatile bool _haveReadDefaultColors;
127 
128  private static volatile byte _defaultColors;
129 
130  private static volatile bool _isOutTextWriterRedirected = false;
131 
132  private static volatile bool _isErrorTextWriterRedirected = false;
133 
134  private static volatile Encoding _inputEncoding = null;
135 
136  private static volatile Encoding _outputEncoding = null;
137 
138  private static volatile bool _stdInRedirectQueried = false;
139 
140  private static volatile bool _stdOutRedirectQueried = false;
141 
142  private static volatile bool _stdErrRedirectQueried = false;
143 
144  private static bool _isStdInRedirected;
145 
146  private static bool _isStdOutRedirected;
147 
148  private static bool _isStdErrRedirected;
149 
150  private static volatile object s_InternalSyncObject;
151 
152  private static volatile object s_ReadKeySyncObject;
153 
154  private static volatile IntPtr _consoleInputHandle;
155 
156  private static volatile IntPtr _consoleOutputHandle;
157 
158  private static object InternalSyncObject
159  {
160  get
161  {
162  if (s_InternalSyncObject == null)
163  {
164  object value = new object();
165  Interlocked.CompareExchange<object>(ref s_InternalSyncObject, value, (object)null);
166  }
167  return s_InternalSyncObject;
168  }
169  }
170 
171  private static object ReadKeySyncObject
172  {
173  get
174  {
175  if (s_ReadKeySyncObject == null)
176  {
177  object value = new object();
178  Interlocked.CompareExchange<object>(ref s_ReadKeySyncObject, value, (object)null);
179  }
180  return s_ReadKeySyncObject;
181  }
182  }
183 
184  private static IntPtr ConsoleInputHandle
185  {
186  [SecurityCritical]
187  get
188  {
189  if (_consoleInputHandle == IntPtr.Zero)
190  {
191  _consoleInputHandle = Win32Native.GetStdHandle(-10);
192  }
193  return _consoleInputHandle;
194  }
195  }
196 
197  private static IntPtr ConsoleOutputHandle
198  {
199  [SecurityCritical]
200  get
201  {
202  if (_consoleOutputHandle == IntPtr.Zero)
203  {
204  _consoleOutputHandle = Win32Native.GetStdHandle(-11);
205  }
206  return _consoleOutputHandle;
207  }
208  }
209 
213  public static bool IsInputRedirected
214  {
215  [SecuritySafeCritical]
216  get
217  {
218  if (_stdInRedirectQueried)
219  {
220  return _isStdInRedirected;
221  }
222  lock (InternalSyncObject)
223  {
224  if (_stdInRedirectQueried)
225  {
226  return _isStdInRedirected;
227  }
228  _isStdInRedirected = IsHandleRedirected(ConsoleInputHandle);
229  _stdInRedirectQueried = true;
230  return _isStdInRedirected;
231  }
232  }
233  }
234 
238  public static bool IsOutputRedirected
239  {
240  [SecuritySafeCritical]
241  get
242  {
243  if (_stdOutRedirectQueried)
244  {
245  return _isStdOutRedirected;
246  }
247  lock (InternalSyncObject)
248  {
249  if (_stdOutRedirectQueried)
250  {
251  return _isStdOutRedirected;
252  }
253  _isStdOutRedirected = IsHandleRedirected(ConsoleOutputHandle);
254  _stdOutRedirectQueried = true;
255  return _isStdOutRedirected;
256  }
257  }
258  }
259 
263  public static bool IsErrorRedirected
264  {
265  [SecuritySafeCritical]
266  get
267  {
268  if (_stdErrRedirectQueried)
269  {
270  return _isStdErrRedirected;
271  }
272  lock (InternalSyncObject)
273  {
274  if (_stdErrRedirectQueried)
275  {
276  return _isStdErrRedirected;
277  }
278  IntPtr stdHandle = Win32Native.GetStdHandle(-12);
279  _isStdErrRedirected = IsHandleRedirected(stdHandle);
280  _stdErrRedirectQueried = true;
281  return _isStdErrRedirected;
282  }
283  }
284  }
285 
288  public static TextReader In
289  {
290  [SecuritySafeCritical]
291  [HostProtection(SecurityAction.LinkDemand, UI = true)]
292  get
293  {
294  if (_in == null)
295  {
296  lock (InternalSyncObject)
297  {
298  if (_in == null)
299  {
300  Stream stream = OpenStandardInput(256);
301  TextReader @in;
302  if (stream == Stream.Null)
303  {
304  @in = StreamReader.Null;
305  }
306  else
307  {
308  Encoding inputEncoding = InputEncoding;
309  @in = TextReader.Synchronized(new StreamReader(stream, inputEncoding, detectEncodingFromByteOrderMarks: false, 256, leaveOpen: true));
310  }
312  _in = @in;
313  }
314  }
315  }
316  return _in;
317  }
318  }
319 
322  public static TextWriter Out
323  {
324  [HostProtection(SecurityAction.LinkDemand, UI = true)]
325  get
326  {
327  if (_out == null)
328  {
329  InitializeStdOutError(stdout: true);
330  }
331  return _out;
332  }
333  }
334 
337  public static TextWriter Error
338  {
339  [HostProtection(SecurityAction.LinkDemand, UI = true)]
340  get
341  {
342  if (_error == null)
343  {
344  InitializeStdOutError(stdout: false);
345  }
346  return _error;
347  }
348  }
349 
355  public static Encoding InputEncoding
356  {
357  [SecuritySafeCritical]
358  get
359  {
360  if (_inputEncoding != null)
361  {
362  return _inputEncoding;
363  }
364  lock (InternalSyncObject)
365  {
366  if (_inputEncoding != null)
367  {
368  return _inputEncoding;
369  }
370  uint consoleCP = Win32Native.GetConsoleCP();
371  _inputEncoding = Encoding.GetEncoding((int)consoleCP);
372  return _inputEncoding;
373  }
374  }
375  [SecuritySafeCritical]
376  set
377  {
378  if (value == null)
379  {
380  throw new ArgumentNullException("value");
381  }
382  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
383  lock (InternalSyncObject)
384  {
385  if (!IsStandardConsoleUnicodeEncoding(value))
386  {
387  uint codePage = (uint)value.CodePage;
388  if (!Win32Native.SetConsoleCP(codePage))
389  {
390  __Error.WinIOError();
391  }
392  }
393  _inputEncoding = (Encoding)value.Clone();
394  _in = null;
395  }
396  }
397  }
398 
404  public static Encoding OutputEncoding
405  {
406  [SecuritySafeCritical]
407  get
408  {
409  if (_outputEncoding != null)
410  {
411  return _outputEncoding;
412  }
413  lock (InternalSyncObject)
414  {
415  if (_outputEncoding != null)
416  {
417  return _outputEncoding;
418  }
419  uint consoleOutputCP = Win32Native.GetConsoleOutputCP();
420  _outputEncoding = Encoding.GetEncoding((int)consoleOutputCP);
421  return _outputEncoding;
422  }
423  }
424  [SecuritySafeCritical]
425  set
426  {
427  if (value == null)
428  {
429  throw new ArgumentNullException("value");
430  }
431  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
432  lock (InternalSyncObject)
433  {
434  if (_out != null && !_isOutTextWriterRedirected)
435  {
436  _out.Flush();
437  _out = null;
438  }
439  if (_error != null && !_isErrorTextWriterRedirected)
440  {
441  _error.Flush();
442  _error = null;
443  }
444  if (!IsStandardConsoleUnicodeEncoding(value))
445  {
446  uint codePage = (uint)value.CodePage;
447  if (!Win32Native.SetConsoleOutputCP(codePage))
448  {
449  __Error.WinIOError();
450  }
451  }
452  _outputEncoding = (Encoding)value.Clone();
453  }
454  }
455  }
456 
462  public static ConsoleColor BackgroundColor
463  {
464  [SecuritySafeCritical]
465  get
466  {
467  bool succeeded;
468  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(throwOnNoConsole: false, out succeeded);
469  if (!succeeded)
470  {
471  return ConsoleColor.Black;
472  }
473  Win32Native.Color c = (Win32Native.Color)(bufferInfo.wAttributes & 0xF0);
474  return ColorAttributeToConsoleColor(c);
475  }
476  [SecuritySafeCritical]
477  set
478  {
479  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
480  Win32Native.Color color = ConsoleColorToColorAttribute(value, isBackground: true);
481  bool succeeded;
482  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(throwOnNoConsole: false, out succeeded);
483  if (succeeded)
484  {
485  short wAttributes = bufferInfo.wAttributes;
486  wAttributes = (short)(wAttributes & -241);
487  wAttributes = (short)((ushort)wAttributes | (ushort)color);
488  Win32Native.SetConsoleTextAttribute(ConsoleOutputHandle, wAttributes);
489  }
490  }
491  }
492 
498  public static ConsoleColor ForegroundColor
499  {
500  [SecuritySafeCritical]
501  get
502  {
503  bool succeeded;
504  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(throwOnNoConsole: false, out succeeded);
505  if (!succeeded)
506  {
507  return ConsoleColor.Gray;
508  }
509  Win32Native.Color c = (Win32Native.Color)(bufferInfo.wAttributes & 0xF);
510  return ColorAttributeToConsoleColor(c);
511  }
512  [SecuritySafeCritical]
513  set
514  {
515  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
516  Win32Native.Color color = ConsoleColorToColorAttribute(value, isBackground: false);
517  bool succeeded;
518  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(throwOnNoConsole: false, out succeeded);
519  if (succeeded)
520  {
521  short wAttributes = bufferInfo.wAttributes;
522  wAttributes = (short)(wAttributes & -16);
523  wAttributes = (short)((ushort)wAttributes | (ushort)color);
524  Win32Native.SetConsoleTextAttribute(ConsoleOutputHandle, wAttributes);
525  }
526  }
527  }
528 
534  public static int BufferHeight
535  {
536  [SecuritySafeCritical]
537  get
538  {
539  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
540  return bufferInfo.dwSize.Y;
541  }
542  set
543  {
544  SetBufferSize(BufferWidth, value);
545  }
546  }
547 
553  public static int BufferWidth
554  {
555  [SecuritySafeCritical]
556  get
557  {
558  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
559  return bufferInfo.dwSize.X;
560  }
561  set
562  {
563  SetBufferSize(value, BufferHeight);
564  }
565  }
566 
571  public static int WindowHeight
572  {
573  [SecuritySafeCritical]
574  get
575  {
576  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
577  return bufferInfo.srWindow.Bottom - bufferInfo.srWindow.Top + 1;
578  }
579  set
580  {
581  SetWindowSize(WindowWidth, value);
582  }
583  }
584 
589  public static int WindowWidth
590  {
591  [SecuritySafeCritical]
592  get
593  {
594  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
595  return bufferInfo.srWindow.Right - bufferInfo.srWindow.Left + 1;
596  }
597  set
598  {
599  SetWindowSize(value, WindowHeight);
600  }
601  }
602 
605  public static int LargestWindowWidth
606  {
607  [SecuritySafeCritical]
608  get
609  {
610  Win32Native.COORD largestConsoleWindowSize = Win32Native.GetLargestConsoleWindowSize(ConsoleOutputHandle);
611  return largestConsoleWindowSize.X;
612  }
613  }
614 
617  public static int LargestWindowHeight
618  {
619  [SecuritySafeCritical]
620  get
621  {
622  Win32Native.COORD largestConsoleWindowSize = Win32Native.GetLargestConsoleWindowSize(ConsoleOutputHandle);
623  return largestConsoleWindowSize.Y;
624  }
625  }
626 
631  public static int WindowLeft
632  {
633  [SecuritySafeCritical]
634  get
635  {
636  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
637  return bufferInfo.srWindow.Left;
638  }
639  set
640  {
642  }
643  }
644 
649  public static int WindowTop
650  {
651  [SecuritySafeCritical]
652  get
653  {
654  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
655  return bufferInfo.srWindow.Top;
656  }
657  set
658  {
660  }
661  }
662 
668  public static int CursorLeft
669  {
670  [SecuritySafeCritical]
671  get
672  {
673  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
674  return bufferInfo.dwCursorPosition.X;
675  }
676  set
677  {
679  }
680  }
681 
687  public static int CursorTop
688  {
689  [SecuritySafeCritical]
690  get
691  {
692  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
693  return bufferInfo.dwCursorPosition.Y;
694  }
695  set
696  {
698  }
699  }
700 
706  public static int CursorSize
707  {
708  [SecuritySafeCritical]
709  get
710  {
711  IntPtr consoleOutputHandle = ConsoleOutputHandle;
712  if (!Win32Native.GetConsoleCursorInfo(consoleOutputHandle, out Win32Native.CONSOLE_CURSOR_INFO cci))
713  {
714  __Error.WinIOError();
715  }
716  return cci.dwSize;
717  }
718  [SecuritySafeCritical]
719  set
720  {
721  if (value < 1 || value > 100)
722  {
723  throw new ArgumentOutOfRangeException("value", value, Environment.GetResourceString("ArgumentOutOfRange_CursorSize"));
724  }
725  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
726  IntPtr consoleOutputHandle = ConsoleOutputHandle;
727  if (!Win32Native.GetConsoleCursorInfo(consoleOutputHandle, out Win32Native.CONSOLE_CURSOR_INFO cci))
728  {
729  __Error.WinIOError();
730  }
731  cci.dwSize = value;
732  if (!Win32Native.SetConsoleCursorInfo(consoleOutputHandle, ref cci))
733  {
734  __Error.WinIOError();
735  }
736  }
737  }
738 
744  public static bool CursorVisible
745  {
746  [SecuritySafeCritical]
747  get
748  {
749  IntPtr consoleOutputHandle = ConsoleOutputHandle;
750  if (!Win32Native.GetConsoleCursorInfo(consoleOutputHandle, out Win32Native.CONSOLE_CURSOR_INFO cci))
751  {
752  __Error.WinIOError();
753  }
754  return cci.bVisible;
755  }
756  [SecuritySafeCritical]
757  set
758  {
759  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
760  IntPtr consoleOutputHandle = ConsoleOutputHandle;
761  if (!Win32Native.GetConsoleCursorInfo(consoleOutputHandle, out Win32Native.CONSOLE_CURSOR_INFO cci))
762  {
763  __Error.WinIOError();
764  }
765  cci.bVisible = value;
766  if (!Win32Native.SetConsoleCursorInfo(consoleOutputHandle, ref cci))
767  {
768  __Error.WinIOError();
769  }
770  }
771  }
772 
779  public static string Title
780  {
781  [SecuritySafeCritical]
782  get
783  {
784  string s = null;
785  int outTitleLength = -1;
786  int titleNative = GetTitleNative(JitHelpers.GetStringHandleOnStack(ref s), out outTitleLength);
787  if (titleNative != 0)
788  {
789  __Error.WinIOError(titleNative, string.Empty);
790  }
791  if (outTitleLength > 24500)
792  {
793  throw new InvalidOperationException(Environment.GetResourceString("ArgumentOutOfRange_ConsoleTitleTooLong"));
794  }
795  return s;
796  }
797  [SecuritySafeCritical]
798  set
799  {
800  if (value == null)
801  {
802  throw new ArgumentNullException("value");
803  }
804  if (value.Length > 24500)
805  {
806  throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_ConsoleTitleTooLong"));
807  }
808  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
809  if (!Win32Native.SetConsoleTitle(value))
810  {
811  __Error.WinIOError();
812  }
813  }
814  }
815 
821  public static bool KeyAvailable
822  {
823  [SecuritySafeCritical]
824  [HostProtection(SecurityAction.LinkDemand, UI = true)]
825  get
826  {
827  if (_cachedInputRecord.eventType == 1)
828  {
829  return true;
830  }
831  Win32Native.InputRecord buffer = default(Win32Native.InputRecord);
832  int numEventsRead = 0;
833  while (true)
834  {
835  if (!Win32Native.PeekConsoleInput(ConsoleInputHandle, out buffer, 1, out numEventsRead))
836  {
837  int lastWin32Error = Marshal.GetLastWin32Error();
838  if (lastWin32Error == 6)
839  {
840  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ConsoleKeyAvailableOnFile"));
841  }
842  __Error.WinIOError(lastWin32Error, "stdin");
843  }
844  if (numEventsRead == 0)
845  {
846  return false;
847  }
848  if (IsKeyDownEvent(buffer) && !IsModKey(buffer))
849  {
850  break;
851  }
852  if (!Win32Native.ReadConsoleInput(ConsoleInputHandle, out buffer, 1, out numEventsRead))
853  {
854  __Error.WinIOError();
855  }
856  }
857  return true;
858  }
859  }
860 
864  public static bool NumberLock
865  {
866  [SecuritySafeCritical]
867  get
868  {
869  short keyState = Win32Native.GetKeyState(144);
870  return (keyState & 1) == 1;
871  }
872  }
873 
877  public static bool CapsLock
878  {
879  [SecuritySafeCritical]
880  get
881  {
882  short keyState = Win32Native.GetKeyState(20);
883  return (keyState & 1) == 1;
884  }
885  }
886 
891  public static bool TreatControlCAsInput
892  {
893  [SecuritySafeCritical]
894  get
895  {
896  IntPtr consoleInputHandle = ConsoleInputHandle;
897  if (consoleInputHandle == Win32Native.INVALID_HANDLE_VALUE)
898  {
899  throw new IOException(Environment.GetResourceString("IO.IO_NoConsole"));
900  }
901  int mode = 0;
902  if (!Win32Native.GetConsoleMode(consoleInputHandle, out mode))
903  {
904  __Error.WinIOError();
905  }
906  return (mode & 1) == 0;
907  }
908  [SecuritySafeCritical]
909  set
910  {
911  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
912  IntPtr consoleInputHandle = ConsoleInputHandle;
913  if (consoleInputHandle == Win32Native.INVALID_HANDLE_VALUE)
914  {
915  throw new IOException(Environment.GetResourceString("IO.IO_NoConsole"));
916  }
917  int mode = 0;
918  bool consoleMode = Win32Native.GetConsoleMode(consoleInputHandle, out mode);
919  mode = ((!value) ? (mode | 1) : (mode & -2));
920  if (!Win32Native.SetConsoleMode(consoleInputHandle, mode))
921  {
922  __Error.WinIOError();
923  }
924  }
925  }
926 
928  public static event ConsoleCancelEventHandler CancelKeyPress
929  {
930  [SecuritySafeCritical]
931  add
932  {
933  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
934  lock (InternalSyncObject)
935  {
936  _cancelCallbacks = (ConsoleCancelEventHandler)Delegate.Combine(_cancelCallbacks, value);
937  if (_hooker == null)
938  {
939  _hooker = new ControlCHooker();
940  _hooker.Hook();
941  }
942  }
943  }
944  [SecuritySafeCritical]
945  remove
946  {
947  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
948  lock (InternalSyncObject)
949  {
950  _cancelCallbacks = (ConsoleCancelEventHandler)Delegate.Remove(_cancelCallbacks, value);
951  if (_hooker != null && _cancelCallbacks == null)
952  {
953  _hooker.Unhook();
954  }
955  }
956  }
957  }
958 
959  [SecuritySafeCritical]
960  private static bool IsHandleRedirected(IntPtr ioHandle)
961  {
962  SafeFileHandle handle = new SafeFileHandle(ioHandle, ownsHandle: false);
963  int fileType = Win32Native.GetFileType(handle);
964  if ((fileType & 2) != 2)
965  {
966  return true;
967  }
968  int mode;
969  bool consoleMode = Win32Native.GetConsoleMode(ioHandle, out mode);
970  return !consoleMode;
971  }
972 
973  [SecuritySafeCritical]
974  private static void InitializeStdOutError(bool stdout)
975  {
976  lock (InternalSyncObject)
977  {
978  if ((!stdout || _out == null) && (stdout || _error == null))
979  {
980  TextWriter textWriter = null;
981  Stream stream = (!stdout) ? OpenStandardError(256) : OpenStandardOutput(256);
982  if (stream == Stream.Null)
983  {
985  }
986  else
987  {
988  Encoding outputEncoding = OutputEncoding;
989  StreamWriter streamWriter = new StreamWriter(stream, outputEncoding, 256, leaveOpen: true);
990  streamWriter.HaveWrittenPreamble = true;
991  streamWriter.AutoFlush = true;
992  textWriter = TextWriter.Synchronized(streamWriter);
993  }
994  if (stdout)
995  {
996  _out = textWriter;
997  }
998  else
999  {
1000  _error = textWriter;
1001  }
1002  }
1003  }
1004  }
1005 
1006  private static bool IsStandardConsoleUnicodeEncoding(Encoding encoding)
1007  {
1008  UnicodeEncoding unicodeEncoding = encoding as UnicodeEncoding;
1009  if (unicodeEncoding == null)
1010  {
1011  return false;
1012  }
1013  if (StdConUnicodeEncoding.CodePage == unicodeEncoding.CodePage)
1014  {
1015  return StdConUnicodeEncoding.bigEndian == unicodeEncoding.bigEndian;
1016  }
1017  return false;
1018  }
1019 
1020  private static bool GetUseFileAPIs(int handleType)
1021  {
1022  switch (handleType)
1023  {
1024  case -10:
1025  if (IsStandardConsoleUnicodeEncoding(InputEncoding))
1026  {
1027  return IsInputRedirected;
1028  }
1029  return true;
1030  case -11:
1031  if (IsStandardConsoleUnicodeEncoding(OutputEncoding))
1032  {
1033  return IsOutputRedirected;
1034  }
1035  return true;
1036  case -12:
1037  if (IsStandardConsoleUnicodeEncoding(OutputEncoding))
1038  {
1039  return IsErrorRedirected;
1040  }
1041  return true;
1042  default:
1043  return true;
1044  }
1045  }
1046 
1047  [SecuritySafeCritical]
1048  private static Stream GetStandardFile(int stdHandleName, FileAccess access, int bufferSize)
1049  {
1050  IntPtr stdHandle = Win32Native.GetStdHandle(stdHandleName);
1051  SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, ownsHandle: false);
1052  if (safeFileHandle.IsInvalid)
1053  {
1054  safeFileHandle.SetHandleAsInvalid();
1055  return Stream.Null;
1056  }
1057  if (stdHandleName != -10 && !ConsoleHandleIsWritable(safeFileHandle))
1058  {
1059  return Stream.Null;
1060  }
1061  bool useFileAPIs = GetUseFileAPIs(stdHandleName);
1062  return new __ConsoleStream(safeFileHandle, access, useFileAPIs);
1063  }
1064 
1065  [SecuritySafeCritical]
1066  private unsafe static bool ConsoleHandleIsWritable(SafeFileHandle outErrHandle)
1067  {
1068  byte b = 65;
1069  int numBytesWritten;
1070  int num = Win32Native.WriteFile(outErrHandle, &b, 0, out numBytesWritten, IntPtr.Zero);
1071  return num != 0;
1072  }
1073 
1076  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1077  public static void Beep()
1078  {
1079  Beep(800, 200);
1080  }
1081 
1089  [SecuritySafeCritical]
1090  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1091  public static void Beep(int frequency, int duration)
1092  {
1093  if (frequency < 37 || frequency > 32767)
1094  {
1095  throw new ArgumentOutOfRangeException("frequency", frequency, Environment.GetResourceString("ArgumentOutOfRange_BeepFrequency", 37, 32767));
1096  }
1097  if (duration <= 0)
1098  {
1099  throw new ArgumentOutOfRangeException("duration", duration, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
1100  }
1101  Win32Native.Beep(frequency, duration);
1102  }
1103 
1106  [SecuritySafeCritical]
1107  public static void Clear()
1108  {
1109  Win32Native.COORD cOORD = default(Win32Native.COORD);
1110  IntPtr consoleOutputHandle = ConsoleOutputHandle;
1111  if (consoleOutputHandle == Win32Native.INVALID_HANDLE_VALUE)
1112  {
1113  throw new IOException(Environment.GetResourceString("IO.IO_NoConsole"));
1114  }
1115  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
1116  int num = bufferInfo.dwSize.X * bufferInfo.dwSize.Y;
1117  int pNumCharsWritten = 0;
1118  if (!Win32Native.FillConsoleOutputCharacter(consoleOutputHandle, ' ', num, cOORD, out pNumCharsWritten))
1119  {
1120  __Error.WinIOError();
1121  }
1122  pNumCharsWritten = 0;
1123  if (!Win32Native.FillConsoleOutputAttribute(consoleOutputHandle, bufferInfo.wAttributes, num, cOORD, out pNumCharsWritten))
1124  {
1125  __Error.WinIOError();
1126  }
1127  if (!Win32Native.SetConsoleCursorPosition(consoleOutputHandle, cOORD))
1128  {
1129  __Error.WinIOError();
1130  }
1131  }
1132 
1133  [SecurityCritical]
1134  private static Win32Native.Color ConsoleColorToColorAttribute(ConsoleColor color, bool isBackground)
1135  {
1136  if ((color & (ConsoleColor)(-16)) != 0)
1137  {
1138  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidConsoleColor"));
1139  }
1140  Win32Native.Color color2 = (Win32Native.Color)color;
1141  if (isBackground)
1142  {
1143  color2 = (Win32Native.Color)((int)color2 << 4);
1144  }
1145  return color2;
1146  }
1147 
1148  [SecurityCritical]
1149  private static ConsoleColor ColorAttributeToConsoleColor(Win32Native.Color c)
1150  {
1151  if ((c & Win32Native.Color.BackgroundMask) != 0)
1152  {
1153  c = (Win32Native.Color)((int)c >> 4);
1154  }
1155  return (ConsoleColor)c;
1156  }
1157 
1161  [SecuritySafeCritical]
1162  public static void ResetColor()
1163  {
1164  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
1165  bool succeeded;
1166  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(throwOnNoConsole: false, out succeeded);
1167  if (succeeded)
1168  {
1169  short attributes = _defaultColors;
1170  Win32Native.SetConsoleTextAttribute(ConsoleOutputHandle, attributes);
1171  }
1172  }
1173 
1188  public static void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop)
1189  {
1190  MoveBufferArea(sourceLeft, sourceTop, sourceWidth, sourceHeight, targetLeft, targetTop, ' ', ConsoleColor.Black, BackgroundColor);
1191  }
1192 
1211  [SecuritySafeCritical]
1212  public unsafe static void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop, char sourceChar, ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
1213  {
1214  if (sourceForeColor < ConsoleColor.Black || sourceForeColor > ConsoleColor.White)
1215  {
1216  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidConsoleColor"), "sourceForeColor");
1217  }
1218  if (sourceBackColor < ConsoleColor.Black || sourceBackColor > ConsoleColor.White)
1219  {
1220  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidConsoleColor"), "sourceBackColor");
1221  }
1222  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
1223  Win32Native.COORD dwSize = bufferInfo.dwSize;
1224  if (sourceLeft < 0 || sourceLeft > dwSize.X)
1225  {
1226  throw new ArgumentOutOfRangeException("sourceLeft", sourceLeft, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1227  }
1228  if (sourceTop < 0 || sourceTop > dwSize.Y)
1229  {
1230  throw new ArgumentOutOfRangeException("sourceTop", sourceTop, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1231  }
1232  if (sourceWidth < 0 || sourceWidth > dwSize.X - sourceLeft)
1233  {
1234  throw new ArgumentOutOfRangeException("sourceWidth", sourceWidth, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1235  }
1236  if (sourceHeight < 0 || sourceTop > dwSize.Y - sourceHeight)
1237  {
1238  throw new ArgumentOutOfRangeException("sourceHeight", sourceHeight, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1239  }
1240  if (targetLeft < 0 || targetLeft > dwSize.X)
1241  {
1242  throw new ArgumentOutOfRangeException("targetLeft", targetLeft, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1243  }
1244  if (targetTop < 0 || targetTop > dwSize.Y)
1245  {
1246  throw new ArgumentOutOfRangeException("targetTop", targetTop, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1247  }
1248  if (sourceWidth == 0 || sourceHeight == 0)
1249  {
1250  return;
1251  }
1252  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
1253  Win32Native.CHAR_INFO[] array = new Win32Native.CHAR_INFO[sourceWidth * sourceHeight];
1254  dwSize.X = (short)sourceWidth;
1255  dwSize.Y = (short)sourceHeight;
1256  Win32Native.COORD bufferCoord = default(Win32Native.COORD);
1257  Win32Native.SMALL_RECT readRegion = default(Win32Native.SMALL_RECT);
1258  readRegion.Left = (short)sourceLeft;
1259  readRegion.Right = (short)(sourceLeft + sourceWidth - 1);
1260  readRegion.Top = (short)sourceTop;
1261  readRegion.Bottom = (short)(sourceTop + sourceHeight - 1);
1262  Win32Native.CHAR_INFO[] array2 = array;
1263  bool flag;
1264  fixed (Win32Native.CHAR_INFO* pBuffer = array2)
1265  {
1266  flag = Win32Native.ReadConsoleOutput(ConsoleOutputHandle, pBuffer, dwSize, bufferCoord, ref readRegion);
1267  }
1268  if (!flag)
1269  {
1270  __Error.WinIOError();
1271  }
1272  Win32Native.COORD cOORD = default(Win32Native.COORD);
1273  cOORD.X = (short)sourceLeft;
1274  Win32Native.Color color = ConsoleColorToColorAttribute(sourceBackColor, isBackground: true);
1275  color |= ConsoleColorToColorAttribute(sourceForeColor, isBackground: false);
1276  short wColorAttribute = (short)color;
1277  for (int i = sourceTop; i < sourceTop + sourceHeight; i++)
1278  {
1279  cOORD.Y = (short)i;
1280  if (!Win32Native.FillConsoleOutputCharacter(ConsoleOutputHandle, sourceChar, sourceWidth, cOORD, out int pNumCharsWritten))
1281  {
1282  __Error.WinIOError();
1283  }
1284  if (!Win32Native.FillConsoleOutputAttribute(ConsoleOutputHandle, wColorAttribute, sourceWidth, cOORD, out pNumCharsWritten))
1285  {
1286  __Error.WinIOError();
1287  }
1288  }
1289  Win32Native.SMALL_RECT writeRegion = default(Win32Native.SMALL_RECT);
1290  writeRegion.Left = (short)targetLeft;
1291  writeRegion.Right = (short)(targetLeft + sourceWidth);
1292  writeRegion.Top = (short)targetTop;
1293  writeRegion.Bottom = (short)(targetTop + sourceHeight);
1294  array2 = array;
1295  fixed (Win32Native.CHAR_INFO* buffer = array2)
1296  {
1297  flag = Win32Native.WriteConsoleOutput(ConsoleOutputHandle, buffer, dwSize, bufferCoord, ref writeRegion);
1298  }
1299  }
1300 
1301  [SecurityCritical]
1302  private static Win32Native.CONSOLE_SCREEN_BUFFER_INFO GetBufferInfo()
1303  {
1304  bool succeeded;
1305  return GetBufferInfo(throwOnNoConsole: true, out succeeded);
1306  }
1307 
1308  [SecuritySafeCritical]
1309  private static Win32Native.CONSOLE_SCREEN_BUFFER_INFO GetBufferInfo(bool throwOnNoConsole, out bool succeeded)
1310  {
1311  succeeded = false;
1312  IntPtr consoleOutputHandle = ConsoleOutputHandle;
1313  if (consoleOutputHandle == Win32Native.INVALID_HANDLE_VALUE)
1314  {
1315  if (!throwOnNoConsole)
1316  {
1317  return default(Win32Native.CONSOLE_SCREEN_BUFFER_INFO);
1318  }
1319  throw new IOException(Environment.GetResourceString("IO.IO_NoConsole"));
1320  }
1321  if (!Win32Native.GetConsoleScreenBufferInfo(consoleOutputHandle, out Win32Native.CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo))
1322  {
1323  bool consoleScreenBufferInfo = Win32Native.GetConsoleScreenBufferInfo(Win32Native.GetStdHandle(-12), out lpConsoleScreenBufferInfo);
1324  if (!consoleScreenBufferInfo)
1325  {
1326  consoleScreenBufferInfo = Win32Native.GetConsoleScreenBufferInfo(Win32Native.GetStdHandle(-10), out lpConsoleScreenBufferInfo);
1327  }
1328  if (!consoleScreenBufferInfo)
1329  {
1330  int lastWin32Error = Marshal.GetLastWin32Error();
1331  if (lastWin32Error == 6 && !throwOnNoConsole)
1332  {
1333  return default(Win32Native.CONSOLE_SCREEN_BUFFER_INFO);
1334  }
1335  __Error.WinIOError(lastWin32Error, null);
1336  }
1337  }
1338  if (!_haveReadDefaultColors)
1339  {
1340  _defaultColors = (byte)(lpConsoleScreenBufferInfo.wAttributes & 0xFF);
1341  _haveReadDefaultColors = true;
1342  }
1343  succeeded = true;
1344  return lpConsoleScreenBufferInfo;
1345  }
1346 
1357  [SecuritySafeCritical]
1358  public static void SetBufferSize(int width, int height)
1359  {
1360  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
1361  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
1362  Win32Native.SMALL_RECT srWindow = bufferInfo.srWindow;
1363  if (width < srWindow.Right + 1 || width >= 32767)
1364  {
1365  throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferLessThanWindowSize"));
1366  }
1367  if (height < srWindow.Bottom + 1 || height >= 32767)
1368  {
1369  throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferLessThanWindowSize"));
1370  }
1371  Win32Native.COORD size = default(Win32Native.COORD);
1372  size.X = (short)width;
1373  size.Y = (short)height;
1374  if (!Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, size))
1375  {
1376  __Error.WinIOError();
1377  }
1378  }
1379 
1389  [SecuritySafeCritical]
1390  public unsafe static void SetWindowSize(int width, int height)
1391  {
1392  if (width <= 0)
1393  {
1394  throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
1395  }
1396  if (height <= 0)
1397  {
1398  throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
1399  }
1400  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
1401  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
1402  bool flag = false;
1403  Win32Native.COORD size = default(Win32Native.COORD);
1404  size.X = bufferInfo.dwSize.X;
1405  size.Y = bufferInfo.dwSize.Y;
1406  if (bufferInfo.dwSize.X < bufferInfo.srWindow.Left + width)
1407  {
1408  if (bufferInfo.srWindow.Left >= 32767 - width)
1409  {
1410  throw new ArgumentOutOfRangeException("width", Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowBufferSize"));
1411  }
1412  size.X = (short)(bufferInfo.srWindow.Left + width);
1413  flag = true;
1414  }
1415  if (bufferInfo.dwSize.Y < bufferInfo.srWindow.Top + height)
1416  {
1417  if (bufferInfo.srWindow.Top >= 32767 - height)
1418  {
1419  throw new ArgumentOutOfRangeException("height", Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowBufferSize"));
1420  }
1421  size.Y = (short)(bufferInfo.srWindow.Top + height);
1422  flag = true;
1423  }
1424  if (flag && !Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, size))
1425  {
1426  __Error.WinIOError();
1427  }
1428  Win32Native.SMALL_RECT srWindow = bufferInfo.srWindow;
1429  srWindow.Bottom = (short)(srWindow.Top + height - 1);
1430  srWindow.Right = (short)(srWindow.Left + width - 1);
1431  if (!Win32Native.SetConsoleWindowInfo(ConsoleOutputHandle, absolute: true, &srWindow))
1432  {
1433  int lastWin32Error = Marshal.GetLastWin32Error();
1434  if (flag)
1435  {
1436  Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, bufferInfo.dwSize);
1437  }
1438  Win32Native.COORD largestConsoleWindowSize = Win32Native.GetLargestConsoleWindowSize(ConsoleOutputHandle);
1439  if (width > largestConsoleWindowSize.X)
1440  {
1441  throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowSize_Size", largestConsoleWindowSize.X));
1442  }
1443  if (height > largestConsoleWindowSize.Y)
1444  {
1445  throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowSize_Size", largestConsoleWindowSize.Y));
1446  }
1447  __Error.WinIOError(lastWin32Error, string.Empty);
1448  }
1449  }
1450 
1460  [SecuritySafeCritical]
1461  public unsafe static void SetWindowPosition(int left, int top)
1462  {
1463  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
1464  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
1465  Win32Native.SMALL_RECT srWindow = bufferInfo.srWindow;
1466  int num = left + srWindow.Right - srWindow.Left + 1;
1467  if (left < 0 || num > bufferInfo.dwSize.X || num < 0)
1468  {
1469  throw new ArgumentOutOfRangeException("left", left, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowPos"));
1470  }
1471  int num2 = top + srWindow.Bottom - srWindow.Top + 1;
1472  if (top < 0 || num2 > bufferInfo.dwSize.Y || num2 < 0)
1473  {
1474  throw new ArgumentOutOfRangeException("top", top, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowPos"));
1475  }
1476  srWindow.Bottom -= (short)(srWindow.Top - top);
1477  srWindow.Right -= (short)(srWindow.Left - left);
1478  srWindow.Left = (short)left;
1479  srWindow.Top = (short)top;
1480  if (!Win32Native.SetConsoleWindowInfo(ConsoleOutputHandle, absolute: true, &srWindow))
1481  {
1482  __Error.WinIOError();
1483  }
1484  }
1485 
1495  [SecuritySafeCritical]
1496  public static void SetCursorPosition(int left, int top)
1497  {
1498  if (left < 0 || left >= 32767)
1499  {
1500  throw new ArgumentOutOfRangeException("left", left, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1501  }
1502  if (top < 0 || top >= 32767)
1503  {
1504  throw new ArgumentOutOfRangeException("top", top, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1505  }
1506  new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand();
1507  IntPtr consoleOutputHandle = ConsoleOutputHandle;
1508  Win32Native.COORD cursorPosition = default(Win32Native.COORD);
1509  cursorPosition.X = (short)left;
1510  cursorPosition.Y = (short)top;
1511  if (!Win32Native.SetConsoleCursorPosition(consoleOutputHandle, cursorPosition))
1512  {
1513  int lastWin32Error = Marshal.GetLastWin32Error();
1514  Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo();
1515  if (left < 0 || left >= bufferInfo.dwSize.X)
1516  {
1517  throw new ArgumentOutOfRangeException("left", left, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1518  }
1519  if (top < 0 || top >= bufferInfo.dwSize.Y)
1520  {
1521  throw new ArgumentOutOfRangeException("top", top, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries"));
1522  }
1523  __Error.WinIOError(lastWin32Error, string.Empty);
1524  }
1525  }
1526 
1527  [DllImport("QCall", CharSet = CharSet.Ansi)]
1528  [SecurityCritical]
1529  [SuppressUnmanagedCodeSecurity]
1530  private static extern int GetTitleNative(StringHandleOnStack outTitle, out int outTitleLength);
1531 
1535  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1536  public static ConsoleKeyInfo ReadKey()
1537  {
1538  return ReadKey(intercept: false);
1539  }
1540 
1541  [SecurityCritical]
1542  private static bool IsAltKeyDown(Win32Native.InputRecord ir)
1543  {
1544  return (ir.keyEvent.controlKeyState & 3) != 0;
1545  }
1546 
1547  [SecurityCritical]
1548  private static bool IsKeyDownEvent(Win32Native.InputRecord ir)
1549  {
1550  if (ir.eventType == 1)
1551  {
1552  return ir.keyEvent.keyDown;
1553  }
1554  return false;
1555  }
1556 
1557  [SecurityCritical]
1558  private static bool IsModKey(Win32Native.InputRecord ir)
1559  {
1560  short virtualKeyCode = ir.keyEvent.virtualKeyCode;
1561  if ((virtualKeyCode < 16 || virtualKeyCode > 18) && virtualKeyCode != 20 && virtualKeyCode != 144)
1562  {
1563  return virtualKeyCode == 145;
1564  }
1565  return true;
1566  }
1567 
1572  [SecuritySafeCritical]
1573  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1574  public static ConsoleKeyInfo ReadKey(bool intercept)
1575  {
1576  int numEventsRead = -1;
1577  Win32Native.InputRecord buffer;
1578  lock (ReadKeySyncObject)
1579  {
1580  if (_cachedInputRecord.eventType == 1)
1581  {
1582  buffer = _cachedInputRecord;
1583  if (_cachedInputRecord.keyEvent.repeatCount == 0)
1584  {
1585  _cachedInputRecord.eventType = -1;
1586  }
1587  else
1588  {
1589  _cachedInputRecord.keyEvent.repeatCount--;
1590  }
1591  }
1592  else
1593  {
1594  while (true)
1595  {
1596  if (!Win32Native.ReadConsoleInput(ConsoleInputHandle, out buffer, 1, out numEventsRead) || numEventsRead == 0)
1597  {
1598  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ConsoleReadKeyOnFile"));
1599  }
1600  short virtualKeyCode = buffer.keyEvent.virtualKeyCode;
1601  if ((IsKeyDownEvent(buffer) || virtualKeyCode == 18) && (buffer.keyEvent.uChar != 0 || !IsModKey(buffer)))
1602  {
1603  ConsoleKey consoleKey = (ConsoleKey)virtualKeyCode;
1604  if (!IsAltKeyDown(buffer))
1605  {
1606  break;
1607  }
1608  if (consoleKey < ConsoleKey.NumPad0 || consoleKey > ConsoleKey.NumPad9)
1609  {
1610  switch (consoleKey)
1611  {
1612  case ConsoleKey.Clear:
1613  case ConsoleKey.PageUp:
1614  case ConsoleKey.PageDown:
1615  case ConsoleKey.End:
1616  case ConsoleKey.Home:
1617  case ConsoleKey.LeftArrow:
1618  case ConsoleKey.UpArrow:
1619  case ConsoleKey.RightArrow:
1620  case ConsoleKey.DownArrow:
1621  case ConsoleKey.Insert:
1622  continue;
1623  }
1624  break;
1625  }
1626  }
1627  }
1628  if (buffer.keyEvent.repeatCount > 1)
1629  {
1630  buffer.keyEvent.repeatCount--;
1631  _cachedInputRecord = buffer;
1632  }
1633  }
1634  }
1635  ControlKeyState controlKeyState = (ControlKeyState)buffer.keyEvent.controlKeyState;
1636  bool shift = (controlKeyState & ControlKeyState.ShiftPressed) != (ControlKeyState)0;
1637  bool alt = (controlKeyState & (ControlKeyState.RightAltPressed | ControlKeyState.LeftAltPressed)) != (ControlKeyState)0;
1638  bool control = (controlKeyState & (ControlKeyState.RightCtrlPressed | ControlKeyState.LeftCtrlPressed)) != (ControlKeyState)0;
1639  ConsoleKeyInfo result = new ConsoleKeyInfo(buffer.keyEvent.uChar, (ConsoleKey)buffer.keyEvent.virtualKeyCode, shift, alt, control);
1640  if (!intercept)
1641  {
1642  Write(buffer.keyEvent.uChar);
1643  }
1644  return result;
1645  }
1646 
1647  private static bool BreakEvent(int controlType)
1648  {
1649  if (controlType == 0 || controlType == 1)
1650  {
1651  ConsoleCancelEventHandler cancelCallbacks = _cancelCallbacks;
1652  if (cancelCallbacks == null)
1653  {
1654  return false;
1655  }
1656  ConsoleSpecialKey controlKey = (controlType != 0) ? ConsoleSpecialKey.ControlBreak : ConsoleSpecialKey.ControlC;
1657  ControlCDelegateData controlCDelegateData = new ControlCDelegateData(controlKey, cancelCallbacks);
1658  WaitCallback callBack = ControlCDelegate;
1659  if (!ThreadPool.QueueUserWorkItem(callBack, controlCDelegateData))
1660  {
1661  return false;
1662  }
1663  TimeSpan timeout = new TimeSpan(0, 0, 30);
1664  controlCDelegateData.CompletionEvent.WaitOne(timeout, exitContext: false);
1665  if (!controlCDelegateData.DelegateStarted)
1666  {
1667  return false;
1668  }
1669  controlCDelegateData.CompletionEvent.WaitOne();
1670  controlCDelegateData.CompletionEvent.Close();
1671  return controlCDelegateData.Cancel;
1672  }
1673  return false;
1674  }
1675 
1676  private static void ControlCDelegate(object data)
1677  {
1678  ControlCDelegateData controlCDelegateData = (ControlCDelegateData)data;
1679  try
1680  {
1681  controlCDelegateData.DelegateStarted = true;
1682  ConsoleCancelEventArgs consoleCancelEventArgs = new ConsoleCancelEventArgs(controlCDelegateData.ControlKey);
1683  controlCDelegateData.CancelCallbacks(null, consoleCancelEventArgs);
1684  controlCDelegateData.Cancel = consoleCancelEventArgs.Cancel;
1685  }
1686  finally
1687  {
1688  controlCDelegateData.CompletionEvent.Set();
1689  }
1690  }
1691 
1694  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1695  public static Stream OpenStandardError()
1696  {
1697  return OpenStandardError(256);
1698  }
1699 
1705  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1706  public static Stream OpenStandardError(int bufferSize)
1707  {
1708  if (bufferSize < 0)
1709  {
1710  throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1711  }
1712  return GetStandardFile(-12, FileAccess.Write, bufferSize);
1713  }
1714 
1717  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1718  public static Stream OpenStandardInput()
1719  {
1720  return OpenStandardInput(256);
1721  }
1722 
1728  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1729  public static Stream OpenStandardInput(int bufferSize)
1730  {
1731  if (bufferSize < 0)
1732  {
1733  throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1734  }
1735  return GetStandardFile(-10, FileAccess.Read, bufferSize);
1736  }
1737 
1740  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1741  public static Stream OpenStandardOutput()
1742  {
1743  return OpenStandardOutput(256);
1744  }
1745 
1751  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1752  public static Stream OpenStandardOutput(int bufferSize)
1753  {
1754  if (bufferSize < 0)
1755  {
1756  throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1757  }
1758  return GetStandardFile(-11, FileAccess.Write, bufferSize);
1759  }
1760 
1766  [SecuritySafeCritical]
1767  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1768  public static void SetIn(TextReader newIn)
1769  {
1770  if (newIn == null)
1771  {
1772  throw new ArgumentNullException("newIn");
1773  }
1774  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
1775  newIn = TextReader.Synchronized(newIn);
1776  lock (InternalSyncObject)
1777  {
1778  _in = newIn;
1779  }
1780  }
1781 
1787  [SecuritySafeCritical]
1788  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1789  public static void SetOut(TextWriter newOut)
1790  {
1791  if (newOut == null)
1792  {
1793  throw new ArgumentNullException("newOut");
1794  }
1795  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
1796  _isOutTextWriterRedirected = true;
1797  newOut = TextWriter.Synchronized(newOut);
1798  lock (InternalSyncObject)
1799  {
1800  _out = newOut;
1801  }
1802  }
1803 
1809  [SecuritySafeCritical]
1810  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1811  public static void SetError(TextWriter newError)
1812  {
1813  if (newError == null)
1814  {
1815  throw new ArgumentNullException("newError");
1816  }
1817  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
1818  _isErrorTextWriterRedirected = true;
1819  newError = TextWriter.Synchronized(newError);
1820  lock (InternalSyncObject)
1821  {
1822  _error = newError;
1823  }
1824  }
1825 
1829  [MethodImpl(MethodImplOptions.NoInlining)]
1830  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1831  public static int Read()
1832  {
1833  return In.Read();
1834  }
1835 
1841  [MethodImpl(MethodImplOptions.NoInlining)]
1842  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1843  public static string ReadLine()
1844  {
1845  return In.ReadLine();
1846  }
1847 
1850  [MethodImpl(MethodImplOptions.NoInlining)]
1851  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1852  public static void WriteLine()
1853  {
1854  Out.WriteLine();
1855  }
1856 
1860  [MethodImpl(MethodImplOptions.NoInlining)]
1861  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1862  public static void WriteLine(bool value)
1863  {
1864  Out.WriteLine(value);
1865  }
1866 
1870  [MethodImpl(MethodImplOptions.NoInlining)]
1871  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1872  public static void WriteLine(char value)
1873  {
1874  Out.WriteLine(value);
1875  }
1876 
1880  [MethodImpl(MethodImplOptions.NoInlining)]
1881  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1882  public static void WriteLine(char[] buffer)
1883  {
1884  Out.WriteLine(buffer);
1885  }
1886 
1898  [MethodImpl(MethodImplOptions.NoInlining)]
1899  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1900  public static void WriteLine(char[] buffer, int index, int count)
1901  {
1902  Out.WriteLine(buffer, index, count);
1903  }
1904 
1908  [MethodImpl(MethodImplOptions.NoInlining)]
1909  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1910  public static void WriteLine(decimal value)
1911  {
1912  Out.WriteLine(value);
1913  }
1914 
1918  [MethodImpl(MethodImplOptions.NoInlining)]
1919  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1920  public static void WriteLine(double value)
1921  {
1922  Out.WriteLine(value);
1923  }
1924 
1928  [MethodImpl(MethodImplOptions.NoInlining)]
1929  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1930  public static void WriteLine(float value)
1931  {
1932  Out.WriteLine(value);
1933  }
1934 
1938  [MethodImpl(MethodImplOptions.NoInlining)]
1939  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1940  public static void WriteLine(int value)
1941  {
1942  Out.WriteLine(value);
1943  }
1944 
1948  [MethodImpl(MethodImplOptions.NoInlining)]
1949  [CLSCompliant(false)]
1950  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1951  public static void WriteLine(uint value)
1952  {
1953  Out.WriteLine(value);
1954  }
1955 
1959  [MethodImpl(MethodImplOptions.NoInlining)]
1960  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1961  public static void WriteLine(long value)
1962  {
1963  Out.WriteLine(value);
1964  }
1965 
1969  [MethodImpl(MethodImplOptions.NoInlining)]
1970  [CLSCompliant(false)]
1971  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1972  public static void WriteLine(ulong value)
1973  {
1974  Out.WriteLine(value);
1975  }
1976 
1980  [MethodImpl(MethodImplOptions.NoInlining)]
1981  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1982  public static void WriteLine(object value)
1983  {
1984  Out.WriteLine(value);
1985  }
1986 
1990  [MethodImpl(MethodImplOptions.NoInlining)]
1991  [HostProtection(SecurityAction.LinkDemand, UI = true)]
1992  public static void WriteLine(string value)
1993  {
1994  Out.WriteLine(value);
1995  }
1996 
2004  [MethodImpl(MethodImplOptions.NoInlining)]
2005  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2006  public static void WriteLine(string format, object arg0)
2007  {
2008  Out.WriteLine(format, arg0);
2009  }
2010 
2019  [MethodImpl(MethodImplOptions.NoInlining)]
2020  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2021  public static void WriteLine(string format, object arg0, object arg1)
2022  {
2023  Out.WriteLine(format, arg0, arg1);
2024  }
2025 
2035  [MethodImpl(MethodImplOptions.NoInlining)]
2036  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2037  public static void WriteLine(string format, object arg0, object arg1, object arg2)
2038  {
2039  Out.WriteLine(format, arg0, arg1, arg2);
2040  }
2041 
2042  [MethodImpl(MethodImplOptions.NoInlining)]
2043  [CLSCompliant(false)]
2044  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2045  public static void WriteLine(string format, object arg0, object arg1, object arg2, object arg3, __arglist)
2046  {
2047  ArgIterator argIterator = new ArgIterator(__arglist);
2048  int num = argIterator.GetRemainingCount() + 4;
2049  object[] array = new object[num];
2050  array[0] = arg0;
2051  array[1] = arg1;
2052  array[2] = arg2;
2053  array[3] = arg3;
2054  for (int i = 4; i < num; i++)
2055  {
2056  array[i] = TypedReference.ToObject(argIterator.GetNextArg());
2057  }
2058  Out.WriteLine(format, array);
2059  }
2060 
2068  [MethodImpl(MethodImplOptions.NoInlining)]
2069  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2070  public static void WriteLine(string format, params object[] arg)
2071  {
2072  if (arg == null)
2073  {
2074  Out.WriteLine(format, null, null);
2075  }
2076  else
2077  {
2078  Out.WriteLine(format, arg);
2079  }
2080  }
2081 
2089  [MethodImpl(MethodImplOptions.NoInlining)]
2090  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2091  public static void Write(string format, object arg0)
2092  {
2093  Out.Write(format, arg0);
2094  }
2095 
2104  [MethodImpl(MethodImplOptions.NoInlining)]
2105  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2106  public static void Write(string format, object arg0, object arg1)
2107  {
2108  Out.Write(format, arg0, arg1);
2109  }
2110 
2120  [MethodImpl(MethodImplOptions.NoInlining)]
2121  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2122  public static void Write(string format, object arg0, object arg1, object arg2)
2123  {
2124  Out.Write(format, arg0, arg1, arg2);
2125  }
2126 
2127  [MethodImpl(MethodImplOptions.NoInlining)]
2128  [CLSCompliant(false)]
2129  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2130  public static void Write(string format, object arg0, object arg1, object arg2, object arg3, __arglist)
2131  {
2132  ArgIterator argIterator = new ArgIterator(__arglist);
2133  int num = argIterator.GetRemainingCount() + 4;
2134  object[] array = new object[num];
2135  array[0] = arg0;
2136  array[1] = arg1;
2137  array[2] = arg2;
2138  array[3] = arg3;
2139  for (int i = 4; i < num; i++)
2140  {
2141  array[i] = TypedReference.ToObject(argIterator.GetNextArg());
2142  }
2143  Out.Write(format, array);
2144  }
2145 
2153  [MethodImpl(MethodImplOptions.NoInlining)]
2154  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2155  public static void Write(string format, params object[] arg)
2156  {
2157  if (arg == null)
2158  {
2159  Out.Write(format, null, null);
2160  }
2161  else
2162  {
2163  Out.Write(format, arg);
2164  }
2165  }
2166 
2170  [MethodImpl(MethodImplOptions.NoInlining)]
2171  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2172  public static void Write(bool value)
2173  {
2174  Out.Write(value);
2175  }
2176 
2180  [MethodImpl(MethodImplOptions.NoInlining)]
2181  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2182  public static void Write(char value)
2183  {
2184  Out.Write(value);
2185  }
2186 
2190  [MethodImpl(MethodImplOptions.NoInlining)]
2191  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2192  public static void Write(char[] buffer)
2193  {
2194  Out.Write(buffer);
2195  }
2196 
2208  [MethodImpl(MethodImplOptions.NoInlining)]
2209  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2210  public static void Write(char[] buffer, int index, int count)
2211  {
2212  Out.Write(buffer, index, count);
2213  }
2214 
2218  [MethodImpl(MethodImplOptions.NoInlining)]
2219  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2220  public static void Write(double value)
2221  {
2222  Out.Write(value);
2223  }
2224 
2228  [MethodImpl(MethodImplOptions.NoInlining)]
2229  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2230  public static void Write(decimal value)
2231  {
2232  Out.Write(value);
2233  }
2234 
2238  [MethodImpl(MethodImplOptions.NoInlining)]
2239  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2240  public static void Write(float value)
2241  {
2242  Out.Write(value);
2243  }
2244 
2248  [MethodImpl(MethodImplOptions.NoInlining)]
2249  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2250  public static void Write(int value)
2251  {
2252  Out.Write(value);
2253  }
2254 
2258  [MethodImpl(MethodImplOptions.NoInlining)]
2259  [CLSCompliant(false)]
2260  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2261  public static void Write(uint value)
2262  {
2263  Out.Write(value);
2264  }
2265 
2269  [MethodImpl(MethodImplOptions.NoInlining)]
2270  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2271  public static void Write(long value)
2272  {
2273  Out.Write(value);
2274  }
2275 
2279  [MethodImpl(MethodImplOptions.NoInlining)]
2280  [CLSCompliant(false)]
2281  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2282  public static void Write(ulong value)
2283  {
2284  Out.Write(value);
2285  }
2286 
2290  [MethodImpl(MethodImplOptions.NoInlining)]
2291  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2292  public static void Write(object value)
2293  {
2294  Out.Write(value);
2295  }
2296 
2300  [MethodImpl(MethodImplOptions.NoInlining)]
2301  [HostProtection(SecurityAction.LinkDemand, UI = true)]
2302  public static void Write(string value)
2303  {
2304  Out.Write(value);
2305  }
2306  }
2307 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
Represents the standard input, output, and error streams for console applications....
Definition: Console.cs:15
static string ReadLine()
Reads the next line of characters from the standard input stream.
Definition: Console.cs:1843
static bool IsInputRedirected
Gets a value that indicates whether input has been redirected from the standard input stream.
Definition: Console.cs:214
static void Write(double value)
Writes the text representation of the specified double-precision floating-point value to the standard...
Definition: Console.cs:2220
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void ResetColor()
Sets the foreground and background console colors to their defaults.
Definition: Console.cs:1162
Describes a set of security permissions applied to code. This class cannot be inherited.
static void Beep()
Plays the sound of a beep through the console speaker.
Definition: Console.cs:1077
static void WriteLine(string format, object arg0, object arg1)
Writes the text representation of the specified objects, followed by the current line terminator,...
Definition: Console.cs:2021
static void WriteLine(string value)
Writes the specified string value, followed by the current line terminator, to the standard output st...
Definition: Console.cs:1992
static void MemoryBarrier()
Synchronizes memory access as follows: The processor executing the current thread cannot reorder inst...
static void WriteLine(string format, params object[] arg)
Writes the text representation of the specified array of objects, followed by the current line termin...
Definition: Console.cs:2070
static bool QueueUserWorkItem(WaitCallback callBack, object state)
Queues a method for execution, and specifies an object containing data to be used by the method....
Definition: ThreadPool.cs:278
static TextWriter Out
Gets the standard output stream.
Definition: Console.cs:323
static new readonly StreamWriter Null
Provides a StreamWriter with no backing store that can be written to, but not read from.
Definition: StreamWriter.cs:55
static void Write(int value)
Writes the text representation of the specified 32-bit signed integer value to the standard output st...
Definition: Console.cs:2250
static void WriteLine(ulong value)
Writes the text representation of the specified 64-bit unsigned integer value, followed by the curren...
Definition: Console.cs:1972
static void WriteLine(double value)
Writes the text representation of the specified double-precision floating-point value,...
Definition: Console.cs:1920
Ensures that all finalization code in derived classes is marked as critical.
int GetRemainingCount()
Returns the number of arguments remaining in the argument list.
static void WriteLine(int value)
Writes the text representation of the specified 32-bit signed integer value, followed by the current ...
Definition: Console.cs:1940
static void Write(bool value)
Writes the text representation of the specified Boolean value to the standard output stream.
Definition: Console.cs:2172
static new readonly StreamReader Null
A T:System.IO.StreamReader object around an empty stream.
Definition: StreamReader.cs:63
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static bool CursorVisible
Gets or sets a value indicating whether the cursor is visible.
Definition: Console.cs:745
UIPermissionWindow
Specifies the type of windows that code is allowed to use.
static void SetBufferSize(int width, int height)
Sets the height and width of the screen buffer area to the specified values.
Definition: Console.cs:1358
static int CursorTop
Gets or sets the row position of the cursor within the buffer area.
Definition: Console.cs:688
Implements a T:System.IO.TextReader that reads characters from a byte stream in a particular encoding...
Definition: StreamReader.cs:13
unsafe TypedReference GetNextArg()
Returns the next argument in a variable-length argument list.
Definition: ArgIterator.cs:50
static unsafe void SetWindowSize(int width, int height)
Sets the height and width of the console window to the specified values.
Definition: Console.cs:1390
static void Write(string format, object arg0, object arg1, object arg2)
Writes the text representation of the specified objects to the standard output stream using the speci...
Definition: Console.cs:2122
static void Clear()
Clears the console buffer and corresponding console window of display information.
Definition: Console.cs:1107
static void WriteLine()
Writes the current line terminator to the standard output stream.
Definition: Console.cs:1852
ConsoleColor
Specifies constants that define foreground and background colors for the console.
Definition: ConsoleColor.cs:5
static int BufferHeight
Gets or sets the height of the buffer area.
Definition: Console.cs:535
static unsafe void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop, char sourceChar, ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
Copies a specified source area of the screen buffer to a specified destination area.
Definition: Console.cs:1212
static Delegate Remove(Delegate source, Delegate value)
Removes the last occurrence of the invocation list of a delegate from the invocation list of another ...
Definition: Delegate.cs:287
static Delegate Combine(Delegate a, Delegate b)
Concatenates the invocation lists of two delegates.
Definition: Delegate.cs:202
static Encoding GetEncoding(int codepage)
Returns the encoding associated with the specified code page identifier.
Definition: Encoding.cs:1249
static int Read()
Reads the next character from the standard input stream.
Definition: Console.cs:1831
static void WriteLine(uint value)
Writes the text representation of the specified 32-bit unsigned integer value, followed by the curren...
Definition: Console.cs:1951
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
static unsafe void SetWindowPosition(int left, int top)
Sets the position of the console window relative to the screen buffer.
Definition: Console.cs:1461
Represents a variable-length argument list; that is, the parameters of a function that takes a variab...
Definition: ArgIterator.cs:7
static unsafe object ToObject(TypedReference value)
Converts the specified TypedReference to an Object.
static int WindowWidth
Gets or sets the width of the console window.
Definition: Console.cs:590
static int CursorSize
Gets or sets the height of the cursor within a character cell.
Definition: Console.cs:707
static void WriteLine(decimal value)
Writes the text representation of the specified T:System.Decimal value, followed by the current line ...
Definition: Console.cs:1910
SecurityAction
Specifies the security actions that can be performed using declarative security.
Implements a T:System.IO.TextWriter for writing characters to a stream in a particular encoding....
Definition: StreamWriter.cs:15
static bool CapsLock
Gets a value indicating whether the CAPS LOCK keyboard toggle is turned on or turned off.
Definition: Console.cs:878
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Describes objects that contain both a managed pointer to a location and a runtime representation of t...
static void Write(char[] buffer, int index, int count)
Writes the specified subarray of Unicode characters to the standard output stream.
Definition: Console.cs:2210
static readonly Stream Null
A Stream with no backing store.
Definition: Stream.cs:562
static void WriteLine(char value)
Writes the specified Unicode character, followed by the current line terminator, value to the standar...
Definition: Console.cs:1872
Represents a UTF-16 encoding of Unicode characters.
static void Write(string format, object arg0, object arg1)
Writes the text representation of the specified objects to the standard output stream using the speci...
Definition: Console.cs:2106
static void WriteLine(string format, object arg0)
Writes the text representation of the specified object, followed by the current line terminator,...
Definition: Console.cs:2006
static void SetIn(TextReader newIn)
Sets the P:System.Console.In property to the specified T:System.IO.TextReader object.
Definition: Console.cs:1768
virtual int CodePage
When overridden in a derived class, gets the code page identifier of the current T:System....
Definition: Encoding.cs:948
virtual int Read()
Reads the next character from the text reader and advances the character position by one character.
Definition: TextReader.cs:202
static ConsoleColor ForegroundColor
Gets or sets the foreground color of the console.
Definition: Console.cs:499
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
static ConsoleCancelEventHandler CancelKeyPress
Occurs when the F:System.ConsoleModifiers.Control modifier key (Ctrl) and either the F:System....
Definition: Console.cs:929
ConsoleSpecialKey
Specifies combinations of modifier and console keys that can interrupt the current process.
static void Write(string format, params object[] arg)
Writes the text representation of the specified array of objects to the standard output stream using ...
Definition: Console.cs:2155
static TextReader In
Gets the standard input stream.
Definition: Console.cs:289
The exception that is thrown when an I/O error occurs.
Definition: IOException.cs:10
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
Controls the permissions related to user interfaces and the Clipboard. This class cannot be inherited...
Definition: UIPermission.cs:9
static TextWriter Error
Gets the standard error output stream.
Definition: Console.cs:338
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
Notifies one or more waiting threads that an event has occurred. This class cannot be inherited.
static void SetOut(TextWriter newOut)
Sets the P:System.Console.Out property to the specified T:System.IO.TextWriter object.
Definition: Console.cs:1789
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
static void WriteLine(long value)
Writes the text representation of the specified 64-bit signed integer value, followed by the current ...
Definition: Console.cs:1961
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
static Stream OpenStandardOutput(int bufferSize)
Acquires the standard output stream, which is set to a specified buffer size.
Definition: Console.cs:1752
static Stream OpenStandardInput(int bufferSize)
Acquires the standard input stream, which is set to a specified buffer size.
Definition: Console.cs:1729
MethodImplOptions
Defines the details of how a method is implemented.
static void WriteLine(string format, object arg0, object arg1, object arg2)
Writes the text representation of the specified objects, followed by the current line terminator,...
Definition: Console.cs:2037
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
Describes the console key that was pressed, including the character represented by the console key an...
virtual void Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
Definition: TextWriter.cs:515
static void Write(object value)
Writes the text representation of the specified object to the standard output stream.
Definition: Console.cs:2292
virtual bool AutoFlush
Gets or sets a value indicating whether the T:System.IO.StreamWriter will flush its buffer to the und...
static int WindowLeft
Gets or sets the leftmost position of the console window area relative to the screen buffer.
Definition: Console.cs:632
static int LargestWindowHeight
Gets the largest possible number of console window rows, based on the current font and screen resolut...
Definition: Console.cs:618
static void Write(string format, object arg0)
Writes the text representation of the specified object to the standard output stream using the specif...
Definition: Console.cs:2091
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
static void Write(char value)
Writes the specified Unicode character value to the standard output stream.
Definition: Console.cs:2182
static void WriteLine(char[] buffer)
Writes the specified array of Unicode characters, followed by the current line terminator,...
Definition: Console.cs:1882
static void WriteLine(char[] buffer, int index, int count)
Writes the specified subarray of Unicode characters, followed by the current line terminator,...
Definition: Console.cs:1900
static void Write(ulong value)
Writes the text representation of the specified 64-bit unsigned integer value to the standard output ...
Definition: Console.cs:2282
static void WriteLine(bool value)
Writes the text representation of the specified Boolean value, followed by the current line terminato...
Definition: Console.cs:1862
static bool IsOutputRedirected
Gets a value that indicates whether output has been redirected from the standard output stream.
Definition: Console.cs:239
static int BufferWidth
Gets or sets the width of the buffer area.
Definition: Console.cs:554
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
static void Write(float value)
Writes the text representation of the specified single-precision floating-point value to the standard...
Definition: Console.cs:2240
static int WindowHeight
Gets or sets the height of the console window area.
Definition: Console.cs:572
Represents a reader that can read a sequential series of characters.
Definition: TextReader.cs:14
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
static void WriteLine(object value)
Writes the text representation of the specified object, followed by the current line terminator,...
Definition: Console.cs:1982
static TextReader Synchronized(TextReader reader)
Creates a thread-safe wrapper around the specified TextReader.
Definition: TextReader.cs:452
static TextWriter Synchronized(TextWriter writer)
Creates a thread-safe wrapper around the specified TextWriter.
Definition: TextWriter.cs:525
static Stream OpenStandardError(int bufferSize)
Acquires the standard error stream, which is set to a specified buffer size.
Definition: Console.cs:1706
virtual void WriteLine()
Writes a line terminator to the text string or stream.
Definition: TextWriter.cs:778
static ConsoleKeyInfo ReadKey(bool intercept)
Obtains the next character or function key pressed by the user. The pressed key is optionally display...
Definition: Console.cs:1574
static string Title
Gets or sets the title to display in the console title bar.
Definition: Console.cs:780
static void SetCursorPosition(int left, int top)
Sets the position of the cursor.
Definition: Console.cs:1496
The exception that is thrown when a method call is invalid for the object's current state.
static ConsoleColor BackgroundColor
Gets or sets the background color of the console.
Definition: Console.cs:463
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
static int WindowTop
Gets or sets the top position of the console window area relative to the screen buffer.
Definition: Console.cs:650
static void Write(long value)
Writes the text representation of the specified 64-bit signed integer value to the standard output st...
Definition: Console.cs:2271
ConsoleKey
Specifies the standard keys on a console.
Definition: ConsoleKey.cs:5
delegate void WaitCallback(object state)
Represents a callback method to be executed by a thread pool thread.
static ConsoleKeyInfo ReadKey()
Obtains the next character or function key pressed by the user. The pressed key is displayed in the c...
Definition: Console.cs:1536
static Encoding OutputEncoding
Gets or sets the encoding the console uses to write output.
Definition: Console.cs:405
static void Write(uint value)
Writes the text representation of the specified 32-bit unsigned integer value to the standard output ...
Definition: Console.cs:2261
static void Write(decimal value)
Writes the text representation of the specified T:System.Decimal value to the standard output stream.
Definition: Console.cs:2230
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
static Stream OpenStandardOutput()
Acquires the standard output stream.
Definition: Console.cs:1741
SecurityPermissionFlag
Specifies access flags for the security permission object.
virtual void Write(char value)
Writes a character to the text string or stream.
Definition: TextWriter.cs:543
static Stream OpenStandardError()
Acquires the standard error stream.
Definition: Console.cs:1695
delegate void ConsoleCancelEventHandler(object sender, ConsoleCancelEventArgs e)
Represents the method that will handle the E:System.Console.CancelKeyPress event of a T:System....
static void WriteLine(float value)
Writes the text representation of the specified single-precision floating-point value,...
Definition: Console.cs:1930
static bool KeyAvailable
Gets a value indicating whether a key press is available in the input stream.
Definition: Console.cs:822
static bool NumberLock
Gets a value indicating whether the NUM LOCK keyboard toggle is turned on or turned off.
Definition: Console.cs:865
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
virtual string ReadLine()
Reads a line of characters from the text reader and returns the data as a string.
Definition: TextReader.cs:303
static int LargestWindowWidth
Gets the largest possible number of console window columns, based on the current font and screen reso...
Definition: Console.cs:606
static void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop)
Copies a specified source area of the screen buffer to a specified destination area.
Definition: Console.cs:1188
static Stream OpenStandardInput()
Acquires the standard input stream.
Definition: Console.cs:1718
static void Beep(int frequency, int duration)
Plays the sound of a beep of a specified frequency and duration through the console speaker.
Definition: Console.cs:1091
static void Write(string value)
Writes the specified string value to the standard output stream.
Definition: Console.cs:2302
static bool? TreatControlCAsInput
Gets or sets a value indicating whether the combination of the F:System.ConsoleModifiers....
Definition: Console.cs:892
static void SetError(TextWriter newError)
Sets the P:System.Console.Error property to the specified T:System.IO.TextWriter object.
Definition: Console.cs:1811
static Encoding InputEncoding
Gets or sets the encoding the console uses to read input.
Definition: Console.cs:356
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition: ThreadPool.cs:14
static int CursorLeft
Gets or sets the column position of the cursor within the buffer area.
Definition: Console.cs:669
static void Write(char[] buffer)
Writes the specified array of Unicode characters to the standard output stream.
Definition: Console.cs:2192
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18
static bool IsErrorRedirected
Gets a value that indicates whether the error output stream has been redirected from the standard err...
Definition: Console.cs:264