counter = 0;
string line;
bool validCheck = true;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(deckFile);
while ((line = file.ReadLine()) != null && validCheck == true)
{
if (line.Split(',')[1] == Form1.tempUsernameOut)
{
validCheck = false;
}
else
{
if (file.EndOfStream)
{
int lineCountDecks = File.ReadAllLines(deckFile).Length + 1; // Makes the variable set to the amount of lines in the deck file + 1.
string deckWriting = lineCountDecks.ToString() + "," + Form1.tempUsernameOut + ",1,,,,,,,,,,,2,,,,,,,,,,,3,,,,,,,,,,,4,,,,,,,,,,,5,,,,,,,,,,,"; // Stores what will be written in the deck file in a variable.
// Writes the contents of the variable "deckWriting" in the deck file.
StreamWriter writeFile = File.AppendText(deckFile);
writeFile.WriteLine(deckWriting);
writeFile.Close();
validCheck = false;
}
}
counter++;
}
file.Close();
這是我到目前爲止,但它不起作用。這是我想要做的。如果文本文件中第一行的第二部分與tempUsernameOut匹配,則不執行任何操作。如果不匹配,請檢查下一行。檢查完所有行後,如果任何行的第二部分與tempUsernameOut不匹配,請將存儲在deckWriting中的行寫入文本文件的末尾。 我從這裏得到了代碼的基礎。謝謝!如果兩個東西不匹配,寫入文件末尾
https://msdn.microsoft.com/en-GB/library/aa287535(v=vs.71).aspx
真棒描述'但它並不work'。你介意給我們一個詳細的描述什麼是不正確的嗎? – ViRuSTriNiTy
@ViRuSTriNiTy如果文本文件爲空,則「if」語句不會寫入文本文件(這是第一個問題)。然後我手動在文本文件中添加一行。如果tempUsernameOut與該行的第二部分相匹配,則表單加載正常,但如果tempUsernameOut與該行的第二部分不匹配,則顯示[此錯誤](https://gyazo.com/96b14f0d8a6ba0cc9289e05de295d4f0)。 – Headshot
@ViRuSTriNiTy明白了。 :) 不管怎麼說,還是要謝謝你。 – Headshot