文件信息上次访问的毫秒数未更新
在方法 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您每隔 5000 毫秒调用一次此操作。 因此,如果操作完成时间小于 1 毫秒,则文件时间戳的毫秒部分不会改变,例如:
尝试更改间隔(例如更改为 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:
Try changing the interval (e.g. to 5003 ms) to see that the millisecond part is updated.