2016-07-28 112 views
-2
 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

+2

真棒描述'但它并不work'。你介意给我们一个详细的描述什么是不正确的吗? – ViRuSTriNiTy

+0

@ViRuSTriNiTy如果文本文件为空,则“if”语句不会写入文本文件(这是第一个问题)。然后我手动在文本文件中添加一行。如果tempUsernameOut与该行的第二部分相匹配,则表单加载正常,但如果tempUsernameOut与该行的第二部分不匹配,则显示[此错误](https://gyazo.com/96b14f0d8a6ba0cc9289e05de295d4f0)。 – Headshot

+0

@ViRuSTriNiTy明白了。 :) 不管怎么说,还是要谢谢你。 – Headshot

回答

2

起初,总是用 “使用” 采用流。这样,即使发生异常,您也一定会关闭流。

你的问题在于你试图写入文件,当它被阅读流阻止。 检查是否需要,当你关闭读数据流,这样的事情

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

更好_File.AppendAllText(deckFile,deckWriting); _ – Steve

+0

谢谢!使用[非常类似的东西](https://gyazo.com/d033c662f4b3690598aa0248e4fd6674)到这(只是一些我更习惯的东西的变化)。我看不出柜台是如何使用的。 – Headshot

0

写入文件或没有,开流写入使用的FileStream也许尝试使用布尔变量。事情是这样的:

using(FileStream fs = new FileStream(@"\\path\\to\\file", FileAccess.Write) 
using StreamWriter sw = new StreamWriter(fs) 
{ 
    sw.WriteLine(deckWriting); 
} 
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仍然为true时才追加行。

  3. 计数器已经占用了行数。

  4. 的String.format看起来更漂亮

+0

谢谢!使用了一些非常相似的东西(只是我更习惯的东西的一些变化)。我看不出柜台是如何使用的。 – Headshot

相关问题