c# - 使用 System.IO.File,检查文本文件是否包含确切的字符串而不是它的一部分
问题描述
所以我正在制作一个程序,允许您注册然后使用控制台登录。由于控制台只能在其中存储数据,只要它没有关闭(据我所知),我使用 .txt 文件。我写了一些代码,当你登录时,它应该检查其他人是否已经在使用你想要的用户名,但问题是,例如,如果某人的用户名是“Manly”,而下一个人希望他们的名字是“Man ” 它表示用户名已经存在,因为“Man”存在于 (Man-“ly”)“Manly”中,如果你知道我在说什么的话。
这是我的 VS 项目中的一个类。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace RegisterAndLogin
{
class Register
{
public string UserName { get; set; }
public string Password { get; set; }
public void Registration()
{
Console.WriteLine("Hello user! Enter the username you want to register with.");
UserName = Console.ReadLine();
string UserNamesInFile = File.ReadAllText(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Usernames.txt");
if (UserNamesInFile.Contains(UserName))
{
Console.WriteLine("The username you entered is already being used. Try a new one.");
while (UserNamesInFile.Contains(UserName))
{
Console.WriteLine("Hello user! Enter the username you want to register with.");
UserName = Console.ReadLine();
}
}
Console.WriteLine("Now enter the password you want to register with.");
Password = Console.ReadLine();
using (StreamWriter file = new StreamWriter(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Usernames.txt", true))
{
file.WriteLine(UserName);
}
using (StreamWriter file1 = new StreamWriter(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Passwords.txt", true))
{
file1.WriteLine(Password);
}
Console.WriteLine("You are succesfully registered");
Console.WriteLine("-----------------------------------------------------------------------");
}
}
}
我建议将代码复制粘贴到 net.framework、控制台 c#、VS 项目中并检查它以了解我在说什么。
这是我拥有的其余代码。
名为登录的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace RegisterAndLogin
{
class Login
{
public string LoginUserName { get; set; }
public string LoginPassword { get; set; }
public void LoginMethod()
{
Console.WriteLine("Hello again user! Enter your username.");
LoginUserName = Console.ReadLine();
string UserNameFileContents = File.ReadAllText(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Usernames.txt");
if (UserNameFileContents.Contains(LoginUserName))
{
Console.WriteLine("Username is correct. ");
}
else
{
Console.WriteLine("Username is incorrect! ");
}
Console.WriteLine("Now enter your password. ");
LoginPassword = Console.ReadLine();
string PasswordFileContents = File.ReadAllText(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Passwords.txt");
if (PasswordFileContents.Contains(LoginPassword))
{
Console.WriteLine("Password is also correct. Press any key to continue to homepage. ");
Console.ReadLine();
}
else
{
Console.WriteLine("Password is incorrect. ");
}
}
}
}
主程序代码 program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterAndLogin
{
class Program
{
static void Main(string[] args)
{
Register registration = new Register();
Login login = new Login();
Console.WriteLine("Enter 1 to register or enter 2 to login.");
string UserNumberInput = Console.ReadLine();
if(UserNumberInput == "1")
{
registration.Registration();
Console.WriteLine("You may now login.");
login.LoginMethod();
}
else if(UserNumberInput == "2")
{
login.LoginMethod();
}
else
{
Console.WriteLine("Enter 1 or 2 only.\nNumber or letter is invalid");
while(UserNumberInput != "1" && UserNumberInput != "2")
{
Console.WriteLine("Enter 1 to register or enter 2 to login.");
UserNumberInput = Console.ReadLine();
Console.WriteLine("--------------------------------------------------");
}
if (UserNumberInput == "1")
{
registration.Registration();
Console.WriteLine("You may now login.");
login.LoginMethod();
}
else if (UserNumberInput == "2")
{
login.LoginMethod();
}
}
Console.ReadLine();
}
}
}```
解决方案
推荐阅读
- vue.js - 安装@vue/cli 时遇到一些问题
- python - df.to_sql 不工作 [HY04] - 无效的 SQL 数据类型 (0) (SQLBindParameter)
- python - 如何在kivy中显示和删除图像?
- python-3.x - SSL:WRONG_VERSION_NUMBER
- prolog - Prolog将字符串列表转换为数字列表
- firebase - 如果用户在运行时突然关闭应用程序,如何停止在 firebase 中执行查询或进程
- reactjs - 如何使用 ant design 动态生成选择组件的选项
- python - 模块“线性模型”的 ModuleNotFoundError
- pandas - 在 Pandas 中查找不适合日期时间格式的行
- python - 最短路径问题:在时间限制内用最少的取货司机取货