文件信息上次访问的毫秒数未更新

发布于 2024-08-01 23:14:54 字数 1305 浏览 2 评论 0原文

在方法 MonitorCallback() 中,我将时间写入文本文件。 写入文件后,我检查文件的 FileInfo 并打印它。

我得到以下结果:

time = 16/08/2009 14:01:46, mili = 307
time = 16/08/2009 14:01:51, mili = 291
time = 16/08/2009 14:01:56, mili = 291
time = 16/08/2009 14:02:01, mili = 291
time = 16/08/2009 14:02:06, mili = 291
time = 16/08/2009 14:02:11, mili = 291

我不明白为什么时间改变了,但毫秒保持固定

 private Timer monitor;
 public Window1()
            {
                InitializeComponent();
                monitor = new Timer(monitorCallback, null, 0, 5000);
            }

    private void monitorCallback(object state)
            {
                string path = @"C:\Test.txt";
                Stream stream = File.OpenWrite(path);
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine(DateTime.Now);
                writer.Close();

                FileInfo fileInfo = new FileInfo(path);
                Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(delegate
                   {
                         Debug.WriteLine( "time = " + fileInfo.LastWriteTimeUtc + ", mili = " +
     fileInfo.LastWriteTimeUtc.Millisecond);


                   }));

                fileInfo = null;

            }

In the method monitorCallback() I write a the time to text file.
after writing the file I check the FileInfo of file and print it.

I have got the following result:

time = 16/08/2009 14:01:46, mili = 307
time = 16/08/2009 14:01:51, mili = 291
time = 16/08/2009 14:01:56, mili = 291
time = 16/08/2009 14:02:01, mili = 291
time = 16/08/2009 14:02:06, mili = 291
time = 16/08/2009 14:02:11, mili = 291

I can't understand why the time is change but the Millisecond is stay fixed

 private Timer monitor;
 public Window1()
            {
                InitializeComponent();
                monitor = new Timer(monitorCallback, null, 0, 5000);
            }

    private void monitorCallback(object state)
            {
                string path = @"C:\Test.txt";
                Stream stream = File.OpenWrite(path);
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine(DateTime.Now);
                writer.Close();

                FileInfo fileInfo = new FileInfo(path);
                Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(delegate
                   {
                         Debug.WriteLine( "time = " + fileInfo.LastWriteTimeUtc + ", mili = " +
     fileInfo.LastWriteTimeUtc.Millisecond);


                   }));

                fileInfo = null;

            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-08-08 23:14:54

您每隔 5000 毫秒调用一次此操作。 因此,如果操作完成时间小于 1 毫秒,则文件时间戳的毫秒部分不会改变,例如:

  • time1 = 16/08/2009 14:01:51.291
  • time2 = 16/08/2009 14:01:56.291 ( = time1 + 5000 ms)
  • 等。

尝试更改间隔(例如更改为 5003 ms)以查看毫秒部分是否已更新。

You are calling this operation exactly every 5000 milliseconds. So if the operation takes less than 1 ms to complete, the millisecond part of the file's timestamp will not change, e.g:

  • time1 = 16/08/2009 14:01:51.291
  • time2 = 16/08/2009 14:01:56.291 (= time1 + 5000 ms)
  • etc.

Try changing the interval (e.g. to 5003 ms) to see that the millisecond part is updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文