2013-05-15 30 views
1

我的要求是要讀取的文件位置檢索的.jpg & .xml文件的列表以及它們的時間戳一起,並將其寫入文件。獲取文件timstamp和管道在C#中的文件列表的輸出

我是C#的新手,到目前爲止我已經能夠獲取文件列表並將輸出寫入文件,但我不知道如何獲取文件的時間戳並將其與列表一起寫入。

我加入了代碼現有的代碼,我需要有對列表中的每個文件的時間戳,這樣我可以在下游使用這些詳細的比較。

請指教。

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

    class Program 
    { 



    static void Main() 
    { 

     TextWriter tw = new StreamWriter(@"C:\test\logfile_c#.txt"); 

     // Put all xml file names in directory into array. 
     string[] array1 = Directory.GetFiles(@"C:\test","*.xml"); 

     // Put all jpg files in directory into array. 
     string[] array2 = Directory.GetFiles(@"C:\test", "*.jpg"); 


     // Display all XML files and write to text file. 
     Console.WriteLine("--- XML Files: ---"); 
     foreach (string name in array1) 
     { 
      Console.WriteLine(name); 
      tw.WriteLine(name); 
      Console.ReadLine(); 
     } 

     // Display all JPG files and write to text file.. 
     Console.WriteLine("--- JPG Files: ---"); 
     foreach (string name in array2) 
     { 
      Console.WriteLine(name); 
      tw.WriteLine(name); 
      Console.ReadLine(); 
     } 

     tw.Close(); 
    } 
    } 

輸出

C:\測試\ chq.xml C:\測試\ img_1.jpg C:\測試\ img_2.jpg

回答

2

取決於哪些時間戳你在追求。您可以使用獲得的創建時間:

new FileInfo(filename).CreationTime; 

這會給你一個DateTime。所以,你可以這樣做:

tw.WriteLine(new FileInfo(name).CreationTime.ToString()); 

.ToString()是可選的 - 你可以用它來控制你的輸出使用的日期/時間格式。如果您想要,也可以使用修改的時間屬性。

+0

串[] array_xml = FileInfo的(@ 「C:\測試」, 「* .XML」)。CreationTime.ToString();此代碼拋出兩個錯誤:System.IO.FileInfo不包含包含兩個參數,System.IO.FileInfo的類型,但作爲變量 – user2385057

+0

創建串串臨時構造=「@ C:\測試」 +「 * .xml「;拋出同樣的錯誤 – user2385057

+0

@ user2385057 - 那行,我給你應該去'foreach'環內。它不會產生一個數組。 '新FileInfo'需要的文件名作爲其構造函數的參數,並返回一個'FileInfo'實例與有關文件的額外的細節。 – PhonicUK

0

正如你所說,你剛剛開始學習,我已經把這個稍作修改的代碼放在一起,告訴你在哪裏可以簡化一些事情 - 我也刪除了讀取和寫入Console

using System.IO; 

namespace Test 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // try to name your variables in a meaningful way. 
      string[] xmlFiles = Directory.GetFiles(@"C:\test", "*.xml"); 
      string[] jpgFiles = Directory.GetFiles(@"C:\test", "*.jpg"); 

      // File.CreateText creates a new file and returns a stream writer. 
      // wrap it in a using statement so you don't have to worry about closing it yourself 
      using (var writer = File.CreateText(@"C:\test\logfile_c#.txt")) 
      { 
       FileInfo fi; 
       foreach (string name in xmlFiles) 
       { 
        // FileInfo instances will give you access to properties 
        // of the file, including the creation date and time. 
        fi = new FileInfo(name); 
        // Use an overload of WriteLine using a format string. 
        writer.WriteLine("file name: {0}, creation time: {1}", name, fi.CreationTime); 
       } 
       foreach (string name in jpgFiles) 
       { 
        fi = new FileInfo(name); 
        writer.WriteLine("file name: {0}, creation time: {1}", name, fi.CreationTime); 
       } 
      } 
     } 
    } 
} 
+0

感謝搶,這是非常有幫助:) – user2385057

0

可以嘗試如下編寫代碼:

//Select directory 
DirectoryInfo dirInfo = new DirectoryInfo("D:\\test"); 

//Get .xml files 
FileInfo[] xmlFiles = dirInfo.GetFiles("*.xml"); 
//Get .jpg files 
FileInfo[] jpgFiles = dirInfo.GetFiles("*.jpg"); 

//Merge files together for convenience 
List<FileInfo> allFiles = new List<FileInfo>(); 
allFiles.InsertRange(allFiles.Count, xmlFiles); 
allFiles.InsertRange(allFiles.Count, jpgFiles); 

//Loop through all files and write their name and creation time to text file 
using (StreamWriter writer = new StreamWriter("D:\\test\\test.txt")) 
{ 
    foreach (FileInfo currentFile in allFiles) 
    { 
     //Format string in desired format 
     string info = string.Format("Name: {0} Creation time: {1}", currentFile.Name, currentFile.CreationTime); 

     writer.WriteLine(info); 
    } 
}