运行后C#阻止CMD自动关闭?

Min*_*lla 1 c# console console-application filestream

我想阻止CMD在运行程序后自动关闭.

码:

static void Main(string[] args)
    {

        FileStream textfsw = new FileStream("fileone.txt", FileMode.Append, FileAccess.Write); // CREATING FILE
        StreamWriter textsw = new StreamWriter(textfsw);

        Console.Write(" Enter your ID: ");
        int ID = int.Parse(Console.ReadLine());
        Console.Write(" Enter your Name: ");
        string Name = Console.ReadLine();
        Console.WriteLine(" Thank you! your logged in as" +Name+ +ID);

        textsw.Close();
        textfsw.Close();

        FileStream textfs = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read); // READING A FILE USING ReadAllLines
        StreamReader textsr = new StreamReader(textfs);

        String[] lines = File.ReadAllLines("fileone.txt");
    }
Run Code Online (Sandbox Code Playgroud)

截图: 运行截图后

我希望在运行后显示以下内容并阻止cmd关闭:

Thank you! your logged in as John Smith 12345
Run Code Online (Sandbox Code Playgroud)

任何答案将不胜感激.

谢谢.

Geo*_*ett 7

您可以使用Console.ReadKey(),等待直到按下一个键(并返回它,但在这种情况下我们不需要它).

这是一种替代Console.ReadLine()Console.Read()其中需要用户按Enter键.


作为协助,当使用文件访问(以及使用IDisposable通常实现的对象)时,您应该使用using语句来确保在所有情况下释放所有资源:

using(FileStream textfs = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read))
{
    using(StreamReader textsr = new StreamReader(textfs))
    {
        // Do things with textfs and textsr
    }
}
Run Code Online (Sandbox Code Playgroud)

另请注意,既然您正在使用File.ReadAllLines它,则不需要上述任何内容,因为它适合您.