我想允許用戶輸入用戶名。用戶名應該存儲在一個文件中(如果該文件不存在,則創建該文件)。但是,如果用戶名已存在於文件中,用戶應該會收到錯誤信息。如何讓用戶輸入用戶名?
一旦完成,用戶應該能夠輸入新的用戶名。
我的問題是,我不知道如何繼續詢問用戶名?
string fileName = ("Usernames.txt");
if (File.Exists(fileName))
Console.WriteLine("Username file already exists!");
else
Console.WriteLine("Username file was created!");
FileStream un = new FileStream("Usernames.txt",
FileMode.Create, FileAccess.Write);
StreamWriter kasutajanimed = new StreamWriter(un);
Usernames.Close();
Console.WriteLine("Username: ");
string uCreation = Console.ReadLine();
bool exists = false;
foreach (string lines in File.ReadAllLines("Usernames.txt"))
{
if (lines == uCreation)
{
Console.WriteLine("Username already exists!");
exists = true;
break;
}
}
if (!exists)
{
File.AppendAllText(@"Usernames.txt", uCreation + Environment.NewLine);
}
歡迎來到我們的社區。您需要調整您的問題,以便詢問具體問題。告訴我們什麼不起作用,或者哪部分代碼需要幫助。 – jgauffin
是的,但現在我找不到一種方法來繼續使用代碼來請求X事物。否則,謝謝! – CrimzonWeb