如果两件事不匹配,则写入文件的末尾

时间:2016-07-28 14:26:47

标签: c#

        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

3 个答案:

答案 0 :(得分:2)

首先,始终对流使用“使用”。这样,即使出现异常,您也可以确保流已关闭。

您的问题在于,当您通过读取流阻止该文件时,您会尝试写入该文件。 使用bool变量检查是否需要写入文件,并在关闭读取流时打开流写入,如下所示

var counter = 0;
string line;
bool validCheck = true;


        // Read the file and display it line by line.
        using (var file = new System.IO.StreamReader(deckFile))
        {
            while ((line = file.ReadLine()) != null && validCheck == true)
            {
                if (line.Split(',')[1] == Form1.tempUsernameOut)
                {
                    validCheck = false;
                    break;
                }
                counter++;
            }
        }

        if (validCheck)
        {
            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.
            using (var writeFile = File.AppendText(deckFile))
            {
                writeFile.WriteLine(deckWriting);
            }
        }

答案 1 :(得分:0)

也许尝试使用FileStream。像这样:

using(FileStream fs = new FileStream(@"\\path\\to\\file", FileAccess.Write)
using StreamWriter sw = new StreamWriter(fs)
{
    sw.WriteLine(deckWriting);
}

答案 2 :(得分:0)

这是更合适的方法:

    counter = 0;
    string line;
    bool validCheck = true;


    // Read the file and display it line by line.
    using( StreamReader reader = File.OpenText(deckFile) )
    {
        while(!reader.EndOfStream && validCheck == true)
        {
            string line = reader.ReadLine();
            if (line.Split(',')[1] == Form1.tempUsernameOut)
            {
                validCheck = false;
            }

            counter++; //Already holds number of lines
        }
    }

    if(validCheck)
    {
        using( StreamWriter writer = File.AppendText(deckFile) )
        {
            string deckWriting = string.Format("{0},{1},1,,,,,,,,,,,2,,,,,,,,,,,3,,,,,,,,,,,4,,,,,,,,,,,5,,,,,,,,,,,", counter, Form1.tempUsernameOut);
            writer.WriteLine(deckWriting);
        }
    }

一些意见:

  1. 使用关闭,因此在您使用完毕后,您的文件“句柄”将会关闭。

  2. 仅当validCheck仍为真时才附加您的行。

  3. 计数器已经保留了行数。

  4. string.Format看起来更漂亮