2011-03-24 240 views
1

下面的程序解析了一個Dwarf Fortress.的所謂的RAW文件。代碼工作正常,但是對於我目前的實現,我需要爲每個文件運行一次程序,每次手動更改源文件。有沒有辦法解析給定文件夾中的所有文本文件?如何從給定文件夾中的所有文件讀取?

(請注意,當前輸出文件與輸入文件位於同一文件夾中,我不太確定如果程序試圖打開它正在寫入的文件時會發生什麼,但它是需要記住的東西)。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // create reader & open file 
      string filePath = @"C:\foo\Dwarf Fortess\creature_domestic.txt"; 
      string destPath = @"C:\foo\Dwarf Fortess\eggCreatures.txt"; 
      string line; 
      // 
      TextReader tr = new StreamReader(filePath); 
      TextWriter tw = new StreamWriter(destPath); 

      // read a line of text 
      while ((line = tr.ReadLine()) != null) 
      { 

         if (line.Contains("[CREATURE:")) 
         { 
          tw.WriteLine(line); 
         } 
         if(line.Contains("[LAYS_EGGS]")) 
         { 
          tr.ReadLine(); 
          tr.ReadLine(); 
          tr.ReadLine(); 
          tw.WriteLine(tr.ReadLine()); 
          tw.WriteLine(tr.ReadLine()); 
         } 


      } 

      // close the stream 
      tr.Close(); 
      tw.Close(); 
     } 
    } 
} 

回答

5

您可以使用Directory.EnumerateFiles獲取目錄中的所有文件並循環瀏覽它們。您可以提供搜索規格以及搜索是否應該遞歸,具體取決於您使用的參數和過載。

順便說一句 - 你應該換在using聲明您的視頻流,以確保妥善處置:

using(TextReader tr = new StreamReader(filePath)) 
{ 
    using(TextWriter tw = new StreamWriter(destPath)) 
    { 
    .... 
    } 
} 
+0

*使用*讓我那.Close()不? – 2011-03-24 22:44:30

+0

@Raven Dreamer - 使用你的代碼時,如果在調用Close之前引發異常,流將不會被關閉。 – Oded 2011-03-24 22:45:32

+0

啊,所以我也可以在最後關閉它們。疑難雜症。 – 2011-03-24 22:47:39

2

檢查Directory.EnumerateFiles它列出了目錄中的所有文件。您可以選擇是否遞歸使用options參數。

然後簡單地加載它給你一個接一個的文件。

1

什麼的.NET版本?您可以對Directory.GetFiles使用linq來排除您要寫入的文件。我現在沒有VS,可能會出現一些錯誤,但你希望能得到這個想法。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string destPath = @"C:\foo\Dwarf Fortess\eggCreatures.txt"; 
      FileInfo fiDest = new FileInfo(destPath); 
      DirectoryInfo diDF = new DirectoryInfo(@"C:\foo\Dwarf Fortress"); 

      var files = from FileInfo f in diDF.GetFiles("*.txt") 
       where f.Name != fiDest.Name 
       select f 
      foreach(FileInfo fiRead in files) 
      { 

      // create reader & open file 
       string filePath = @"C:\foo\Dwarf Fortess\creature_domestic.txt"; 
       string line; 
       // 
       TextReader tr = fiRead.OpenText(); 
       TextWriter tw = new StreamWriter(destPath); 

       // read a line of text 
       while ((line = tr.ReadLine()) != null) 
       { 

          if (line.Contains("[CREATURE:")) 
          { 
           tw.WriteLine(line); 
          } 
          if(line.Contains("[LAYS_EGGS]")) 
          { 
           tr.ReadLine(); 
           tr.ReadLine(); 
           tr.ReadLine(); 
           tw.WriteLine(tr.ReadLine()); 
           tw.WriteLine(tr.ReadLine()); 
          } 


       } 

       // close the stream 
       tr.Close(); 
       tw.Close(); 
      } 
     } 
    } 
} 
相關問題