从文本文件读取字符串

时间:2019-12-08 18:01:00

标签: c#

所以我基本上是在尝试制作简单的登录或注册控制台应用程序,但是我认为我无法打开文本文件,因为每次我输入正确的用户名都会关闭该应用程序。这是我认为应该起作用的代码。

Console.WriteLine("Username:");
                string userNameN = Console.ReadLine();
                string username = System.IO.File.ReadAllText(@"C:\test.txt");

            if (username == userNameN)

            {
                Console.Clear();
                Console.WriteLine("Correct username");
            }
            else
            {
                Console.WriteLine("Incorrect username program will close");
                System.Threading.Thread.Sleep(1000);
                Environment.Exit(0);
            }

            System.Threading.Thread.Sleep(1000);

1 个答案:

答案 0 :(得分:1)

由于代码执行结束而发生。如果要等到下一个输入,则添加Console.ReadLine();。在Console.WriteLine(“ Correct username”)之后;

现在它将等待下一次用户输入。