创建基于命令的(即控制台)应用程序以从文本文件C# [英] Create a Command-based (i.e. console) application to read from Text file C#
本文介绍了创建基于命令的(即控制台)应用程序以从文本文件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
应用程序请求用户名和密码。应用程序匹配来自已保存的登录名称及其在名为login.txt的文本文件中提供的密码的登录名和密码。如果用户名和密码正确,那么应用程序{do something ....}
the application ask for user name and password. The application matches the login name and password from the already saved login names and their password given in a text file called "login.txt". If the user name and password are correct then the application { do something ....}
我不想使用包含,如果我在textfile中写这个格式,用户名:密码,如何替换,而不是包含???
I don't want to use contain, If I wrote in the textfile in this format, username:password, how can replace that instead of contain ???
using (StreamReader sr = File.OpenText("E:\\login.txt"))
{
string[] lines = File.ReadAllLines("E:\\login.txt");
{
if (lines.Contains(Username) && lines.Contains(Password))
{
.......
}
推荐答案
使用 String.Split
using (StreamReader sr = File.OpenText("E:\\login.txt"))
{
string[] lines = File.ReadAllLines("E:\\login.txt");
foreach (string line in lines)
{
string[] userPassword = line.Split(new string[] { ":" }, StringSplitOptions.None);
if (userPassword[0] == Username && userPassword[1] == Password)
{
//.....
}
}
}
这篇关于创建基于命令的(即控制台)应用程序以从文本文件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文