2011-06-08 76 views
0

我有收到這個事件的輸出去到一個文本文件,我想它可能是做的「文件」問題的價值問題輸出文本到文本文件從C#

 private void button1_Click(object sender, EventArgs e) 
     { 

      var file = File.AppendText(@"c:\output.txt"); 

      StreamReader sr = new StreamReader(@"c:\filename.txt"); 
      Regex reg = new Regex(@"\w\:(.(?!\:))+"); 
      List<string> parsedStrings = new List<string>(); 
      while (sr.EndOfStream) 
      { 
       parsedStrings.Add(reg.Match(sr.ReadLine()).Value); 
      } 

     } 
    } 
} 
+0

你有,實際上是使用「文件」的任何代碼變量輸出任何文字?我看到讀入輸入文件的代碼,但沒有相應的寫入輸出文件。 – 2011-06-08 19:22:29

+0

@Matt Hamsmith編號:/,我需要輸出列表到一個文件 – James 2011-06-08 19:24:11

回答

1

File.AppendText(@"c:\output.txt");返回StreamWriter。我不知道你在寫什麼。您只需將項目添加到List<String>。看起來你忘了撥打電話file.Write()。在這種情況下,您不需要List<String>

你可以做

while (sr.EndOfStream) 
{ 
    file.WriteLine(reg.Match(sr.ReadLine()).Value); 
} 

,或者如果你需要的List<String>

,那麼你可以在while循環後嘗試

parsedStrings.ForEach(s => file.WriteLine(s)); 

+0

我想在列表中輸出這些項目到一個文件 – James 2011-06-08 19:23:02

0

試着這麼做:

using (StreamWriter sw = File.AppendText(@"c:\output.txt")) 
{ 
    StreamReader sr = new StreamReader(@"c:\filename.txt"); 
    Regex reg = new Regex(@"\w\:(.(?!\:))+"); 

    while (sr.EndOfStream) 
     { 
      sw.WriteLine(reg.Match(sr.ReadLine()).Value); 
     } 
}