Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何查找/比较文本文件中的特定行?使用C#_C# - Fatal编程技术网

如何查找/比较文本文件中的特定行?使用C#

如何查找/比较文本文件中的特定行?使用C#,c#,C#,我有一个小企业的程序,在表单中您可以添加新用户,新用户名将添加到名为:employees.txt的文本文件中,如下所示: User 1 User 2 User 3 User 4 (用户之间没有空格) 在第二个表单中,当您想要关闭应用程序时,您需要在文本框中写入哪个用户想要关闭应用程序,然后将文本框字符串与employees.txt中的名称进行比较,如果它们匹配,应用程序将关闭。 如何比较文本文件中的特定行 或者我如何做到这一点?首先使用file.ReadAllLines将文件读入这样的数组 st

我有一个小企业的程序,在
表单中
您可以添加新用户,新用户名将添加到名为:
employees.txt
的文本文件中,如下所示:

User 1

User 2

User 3

User 4

(用户之间没有空格)

在第二个
表单
中,当您想要关闭应用程序时,您需要在
文本框
中写入哪个用户想要关闭应用程序,然后将
文本框
字符串与
employees.txt
中的名称进行比较,如果它们匹配,应用程序将关闭。 如何比较文本文件中的特定行


或者我如何做到这一点?

首先使用file.ReadAllLines将文件读入这样的数组

string[] users = System.IO.File.ReadAllLines("employees.txt");
if(users.Any(u=> u == username)) Application.Exit(); // changed assignment to equality comparison
见-


然后使用各种linq方法(如Any、Where等)搜索数组

小企业:-)为什么要使用文本文件而不是可以共享的简单数据库?有很多免费软件可以让事情变得简单。@DStanley我有点新的C语言编程,我不知道如何使用数据库之类的东西,所以我这样做是因为我认为这样的方法比较容易。请同时考虑使用字符串的可能性。比较和指定String Simulion。whitespace@ironstone13我是c#编程的新手,如何使用trim函数?请在调用此方法之前检查字符串是否为null,以避免获取NullReferenceException