简体   繁体   中英

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. 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.

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.

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. 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]

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM