2016-07-28 41 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