System.Console.ReadLine 方法

方法描述

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

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

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static string ReadLine()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.String 输入流中的下一行字符;如果没有更多的可用行,则为 null。

提示和注释

行被定义为后跟回车符(十六进制 0x000d)、换行符(十六进制 0x000a)或 Environment.NewLine 属性值的字符序列。 返回的字符串不包含终止字符。

如果此方法引发 OutOfMemoryException,则读取器在基础 Stream 的位置会前移若干字符,前移的字符数就是该方法能够读取的字符数,但是会放弃已经读入内部 ReadLine 缓冲区的字符。 因为无法更改读取器在流中的位置,所以已经读取的字符是无法恢复的,只能通过重新初始化 TextReader 访问它们。 如果流内的初始位置未知,或者流不支持查找,则也需要重新初始化基础 Stream。

为避免这种情况并产生可靠的代码,应使用 Read 方法,并将已读取的字符存储在预先分配的缓冲区内。

如果在方法从控制台读取输入时按 CTRL + Z 字符,该方法将返回 null。 这使用户能够在循环中调用 ReadLine 方法时防止进一步的键盘输入。 下面的示例阐释此方案。

C#

VB

复制

using System;

public class Example

{

public static void Main()

{

string line;

Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):");

Console.WriteLine();

do {

Console.Write(" ");

line = Console.ReadLine();

if (line != null)

Console.WriteLine(" " + line);

} while (line != null);

}

}

// The following displays possible output from this example:

// Enter one or more lines of text (press CTRL+Z to exit):

//

// This is line #1.

// This is line #1.

// This is line #2

// This is line #2

// ^Z

//

// >

注意

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

System.Console.ReadLine 方法例子

然后使用 Console.ReadLine 方法来读取该文件中的每一行,将每处四个空格的序列替换为制表符,并使用 Console.WriteLine 方法将结果写入输出文件。

using System;
using System.IO;

public class InsertTabs {
    private const int tabSize = 4;
    private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
    public static int Main(string[] args) {
        StreamWriter writer = null;

        if (args.Length < 2) {
            Console.WriteLine(usageText);
            return 1;
        }

        try {
            // Attempt to open output file.
            writer = new StreamWriter(args[1]);
            // Redirect standard output from the console to the output file.
            Console.SetOut(writer);
            // Redirect standard input from the console to the input file.
            Console.SetIn(new StreamReader(args[0]));
        }
        catch(IOException e) {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;            
        }
        string line;
        while ((line = Console.ReadLine()) != null) {
            string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
            Console.WriteLine(newLine);
        }
        writer.Close();
        // Recover the standard output stream so that a 
        // completion message can be displayed.
        StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
        Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);
        return 0;
    }
}

异常

异常 异常描述
IOException 发生了 I/O 错误。
OutOfMemoryException 内存不足,无法为返回的字符串分配缓冲区。
ArgumentOutOfRangeException 下一行字符中的字符数大于 Int32.MaxValue。

命名空间

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 系统要求。