2016-07-28 5 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(); 

ここまではこれまでの内容ですが、動作しません。ここに私がやろうとしていることがあります。テキストファイルの最初の行の2番目のセクションがtempUsernameOutと一致する場合、何もしないでください。一致しない場合は、次の行を確認してください。すべての行をチェックした後、いずれかの行の2番目のセクションがtempUsernameOutと一致しない場合は、deckWritingに格納されている行をテキストファイルの末尾に書き込んでください。 私はここからコードの基盤を得ました。ありがとう!まず2つのものが一致しない場合にファイルの末尾に書き込む

https://msdn.microsoft.com/en-GB/library/aa287535(v=vs.71).aspx

+2

素晴らしい説明を 'に見えますが、それはwork'しません。正確に動作しないものの詳細な説明を教えていただけますか? – ViRuSTriNiTy

+0

@ViRuSTriNiTyテキストファイルが空の場合、 "if"ステートメントはテキストファイルに書き込まれません(これが最初の問題です)。私は手動でテキストファイルに行を追加しました。 tempUsernameOutがその行の2番目のセクションと一致する場合、フォームは正常にロードされますが、tempUsernameOutがその行の2番目のセクションと一致しない場合、[このエラー](https://gyazo.com/96b14f0d8a6ba0cc9289e05de295d4f0)が表示されます。 – Headshot

+0

@ViRuSTriNiTy今すぐ使用できました。 :) とにかくありがとう。 – Headshot

答えて

2

、常にストリームで "使用" を使用します。このようにすれば、例外が発生してもストリームが閉じられることが保証されます。

ストリームを読み込んでブロックされたときにファイルに書き込もうとしています。あなたは多分のFileStreamを使用してみてください、あなたはこの

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

のように流れ、何かを読んで閉じたとき、書き込み用のファイルかどうか、およびオープンストリームに書き込む必要があるかどうかをチェックするための 使用するbool変数。このような何か:

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

関連する問題