我正在构建一个实用程序,它需要从一个文本文件中获取修改日期的属性并将其复制到另一个文本文件中。我更喜欢使用数组来存储文件的日期修改属性。以下是我到目前为止的情况:
class NewTime
{
public DateTime Current { get; set; }
}
static void Main(string[] args)
{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(args[0]);
System.IO.StreamWriter filewriter = new System.IO.StreamWriter(args[1], false);
while ((line = file.ReadLine()) != null)
{
Thread.Sleep(10);
string [] pieces = line.Split(new char[] { '|' });
if(pieces.Length == 2)
{
DateTime outDate;
if(DateTime.TryParse(pieces[1], out outDate))
{
string[] listOfFiles = { @"D:\blahB.txt", @"C:\blahA.txt"};
string[] outputLines = listOfFiles.Select(file => file + " | " + file.GetLastWriteTime(file)).ToArray();
file.WriteAllLines("verifications.txt", outputLines);
//string outputstring = string.Format(" {0:yyyy-MM-dd-hh-mm-ss-ff-tt}", DateTime.Now);
//filewriter.WriteLine(pieces[0] + "|" + outputstring);
}
else
filewriter.WriteLine(line);
}
else
filewriter.WriteLine(line);
System.Console.WriteLine(line);
counter++;
}
System.Console.WriteLine(LastWriteLine+"blahblah");
file.Close();
filewriter.Close();
System.Console.ReadLine();
}
public static string LastWriteLine { get; set; }
}
}
另外,我不需要打开第一个文件,我从中获取日期修改后的属性。
答案 0 :(得分:2)
您可以使用File.GetLastWriteTime获取文件的最后修改日期而不打开它:
string[] listOfFiles = {@"C:\bin\1.txt", @"C:\bin\desktop.png", @"C:\main.c"};
string[] outputLines = listOfFiles.Select(file => file + " | " + File.GetLastWriteTime(file)).ToArray();
File.WriteAllLines("output.txt", outputLines);
然后你会在output.txt输出这样的东西:
C:\bin\1.txt | 9/12/2014 12:49:54 PM
C:\bin\desktop.png | 9/6/2014 3:11:27 AM
C:\main.c | 11/24/2013 7:14:38 PM