[英]How to get “date modified” file properties to be copied line by line onto text file?
I am working on a code that copies information line by line from one text file and pastes it onto another. 我正在编写一个代码,它从一个文本文件中逐行复制信息,然后将其粘贴到另一个文件中。 Each line contains "|"
每行包含“ |” and after that symbol the timestamp of the date modified of each line is displayed.
在该符号之后,将显示每行修改日期的时间戳。 I am having trouble with finding a way that will allow me to access the date modified property from a build server when I run my utility and replaces the old date modified in the old text file with the new date modified property in the new text file.
我在寻找一种方法时遇到了麻烦,该方法将允许我在运行实用程序时从构建服务器访问日期已修改的属性,并用新文本文件中的新的日期已修改的属性替换旧文本文件中已修改的旧日期。 Here is what I have so far:
这是我到目前为止的内容:
class Program
{
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 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++;
}
file.Close();
filewriter.Close();
System.Console.ReadLine();
}
}
The portion in between the stars was my first attempt, but that didn't give me what I want. 星星之间的部分是我的第一次尝试,但这没有给我我想要的东西。 It simply replaced the old time with the current time of when I ran the utility on my computer.
它只是用我在计算机上运行实用程序的当前时间代替了过去的时间。
Any help is appreciated =) 任何帮助表示赞赏=)
If I'm understanding correctly, its looks like a timing problem: You want to replace the old date modified in the old text file with the new date modified property in the new text file. 如果我正确理解的话,它看起来像是时序问题:您想用新文本文件中的new date Modify属性替换旧文本文件中修改的旧日期。 But DateTime.Now is not close enough, and you can't get the filesystem-generated DateModified (actually LastWriteTime) until the file has been saved on the file system.
但是DateTime.Now距离不够近,在将文件保存到文件系统之前,您无法获得文件系统生成的DateModified(实际上是LastWriteTime)。
If so, since you have to flush and close the new file for the file system to write the LastModified value, even re-reading the LastAccessTime on the newly created file FileInfo may not give you the value you are after. 如果是这样,由于必须刷新并关闭文件系统的新文件才能写入LastModified值,因此即使重新读取新创建的文件FileInfo上的LastAccessTime也可能无法获得您想要的值。
It's a little messy because NTFS updates to the last write access time for a file can take an indeterminate amount of time to resolve after the last access. 这有点混乱,因为NTFS更新文件的上次写访问时间可能花费不确定的时间来解决上次访问后的时间。 Even FAT systems have a write time resolution of ~2 seconds, according to Windows SDK [ http://msdn.microsoft.com/en-us/library/windows/desktop/ms724933(v=vs.85).aspx]
根据Windows SDK [ http://msdn.microsoft.com/en-us/library/windows/desktop/ms724933(v=vs.85).aspx] ,即使FAT系统的写入时间分辨率也约为2秒。
So, if replacing the old time with the current time (as you are doing) is not close enough, you would need to complete your initial file-creation loop, then derive a FileSystemInfo object on the file and call its Refresh method to get the latest value, and then re-write the value (LastWriteTime or LastAccessTime) into the file. 因此,如果用当前时间替换旧时间(如您所做的)还不够接近,则需要完成初始文件创建循环,然后在文件上派生FileSystemInfo对象并调用其Refresh方法来获取最新值,然后将值(LastWriteTime或LastAccessTime)重新写入文件中。
Refer to .Net Framework documentation for FileSystemInfo.LastAccessTime property for details: http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.lastaccesstime(v=vs.110).aspx 有关详细信息,请参阅.Net Framework文档中的FileSystemInfo.LastAccessTime属性: http : //msdn.microsoft.com/zh-cn/library/system.io.filesysteminfo.lastaccesstime( v=vs.110) .aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.