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