我的要求是要讀取的文件位置檢索的.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
串[] array_xml = FileInfo的(@ 「C:\測試」, 「* .XML」)。CreationTime.ToString();此代碼拋出兩個錯誤:System.IO.FileInfo不包含包含兩個參數,System.IO.FileInfo的類型,但作爲變量 – user2385057
創建串串臨時構造=「@ C:\測試」 +「 * .xml「;拋出同樣的錯誤 – user2385057
@ user2385057 - 那行,我給你應該去'foreach'環內。它不會產生一個數組。 '新FileInfo'需要的文件名作爲其構造函數的參數,並返回一個'FileInfo'實例與有關文件的額外的細節。 – PhonicUK