System.Console.Read 方法

方法描述

从标准输入流读取下一个字符。

语法定义(C# System.Console.Read 方法 的用法)

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static int Read()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Int32 输入流中的下一个字符;如果当前没有更多的字符可供读取,则为负一 (-1)。

提示和注释

在键入输入字符时,Read 方法会阻止其返回;该方法在您按 Enter 键时终止。 按 Enter 会在输入内容后面追加一个与平台有关的行终止序列(例如,Windows 追加一个回车符和换行符序列)。 对 Read 方法的后续调用一次检索输入中的一个字符。 检索完最后一个字符后,Read 会再次阻止其返回,并重复上述循环。

注意,只有执行以下操作之一才能获取属性值 -1:同时按修改键 Control 和控制台键 Z (Ctrl+Z),此按键组合发出到达文件尾条件;按发出到达文件尾条件的等效键,例如 Windows 中的 F6 功能键;或者将输入流重定向到具有实际的文件尾字符的源,例如文本文件。

使用 ReadLine 方法或使用 KeyAvailable 属性和 ReadKey 方法比使用 Read 方法更可取。

注意

应用到此类型或成员的 HostProtectionAttribute 特性具有以下 Resources 属性值:UI。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标、键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护特性。

System.Console.Read 方法例子

下面的示例说明 Read 方法。

// This example demonstrates the Console.Read() method.
using System;

class Sample 
{
    public static void Main() 
    {
    string m1 = "\nType a string of text then press Enter. " +
                "Type '+' anywhere in the text to quit:\n";
    string m2 = "Character '{0}' is hexadecimal 0x{1:x4}.";
    string m3 = "Character     is hexadecimal 0x{0:x4}.";
    char ch;
    int x;
//
    Console.WriteLine(m1);
    do  
        {
        x = Console.Read();
        try 
            {
            ch = Convert.ToChar(x);
            if (Char.IsWhiteSpace(ch))
               {
               Console.WriteLine(m3, x);
               if (ch == 0x0a) 
                   Console.WriteLine(m1);
               }
            else
               Console.WriteLine(m2, ch, x);
            }
        catch (OverflowException e) 
            {
            Console.WriteLine("{0} Value read = {1}.", e.Message, x);
            ch = Char.MinValue;
            Console.WriteLine(m1);
            }
        } while (ch != '+');
    }
}
/*
This example produces the following results:

Type a string of text then press Enter. Type '+' anywhere in the text to quit:

The quick brown fox.
Character 'T' is hexadecimal 0x0054.
Character 'h' is hexadecimal 0x0068.
Character 'e' is hexadecimal 0x0065.
Character     is hexadecimal 0x0020.
Character 'q' is hexadecimal 0x0071.
Character 'u' is hexadecimal 0x0075.
Character 'i' is hexadecimal 0x0069.
Character 'c' is hexadecimal 0x0063.
Character 'k' is hexadecimal 0x006b.
Character     is hexadecimal 0x0020.
Character 'b' is hexadecimal 0x0062.
Character 'r' is hexadecimal 0x0072.
Character 'o' is hexadecimal 0x006f.
Character 'w' is hexadecimal 0x0077.
Character 'n' is hexadecimal 0x006e.
Character     is hexadecimal 0x0020.
Character 'f' is hexadecimal 0x0066.
Character 'o' is hexadecimal 0x006f.
Character 'x' is hexadecimal 0x0078.
Character '.' is hexadecimal 0x002e.
Character     is hexadecimal 0x000d.
Character     is hexadecimal 0x000a.

Type a string of text then press Enter. Type '+' anywhere in the text to quit:

^Z
Value was either too large or too small for a character. Value read = -1.

Type a string of text then press Enter. Type '+' anywhere in the text to quit:

+
Character '+' is hexadecimal 0x002b.

*/

异常

异常 异常描述
IOException 发生了 I/O 错误。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。