C# - 为什么不System.IO.File.GetLastAccessTime返回一个预期值时,未找到该文件? [英] C# -Why does System.IO.File.GetLastAccessTime return an expected value when the file is not found?

查看:575
本文介绍了C# - 为什么不System.IO.File.GetLastAccessTime返回一个预期值时,未找到该文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,说明你的看法。

  1。 DateTime的DT = System.IO.File.GetLastAccessTime(C:\\There_is_no_such_file.txt); 
2. DateTime的DT = System.IO.File.GetLastAccessTime();




  1. 如果在path参数描述的文件不存在这个方法返回午夜12:00 1月1日,公元1601年(CE)协调世界时(UTC),调整为本地时间。


  2. 在第二形势参数异常。




为什么在第一种情况下FileNotFoundException异常(或不便。simmilar)不甩?


解决方案

这是记录的行为。从MSDN库主题中的备注部分:



如果path参数描述的文件不存在,这个方法返回午夜12点,1月1日公元1601(CE)协调世界时(UTC),调整为本地时间。



当你传递一个空字符串,你得到的异常是一个是通过检查,如果传递的字符串是一个有效的路径名代码生成的。这是公平的,这将是在程序中的bug。



所以它不是被忽略或误完成的代码是明确的。它使用用FindFirstFile()API函数来定位文件。如果失败,它会检查Windows的错误。并明确忽略的找不到文件,找不到路径和驱动器占用的错误。



要注意的是使用File.Exists不要说提供的解决方案实际上防止了这个问题。 Windows是一个多任务的操作系统。你的线程可能会予占权的存在,并调用另一个进程可能会删除该文件后。当你的线程重新获得CPU,你还是会得到虚假的日期。



唯一可以保证的方式得到一个准确的日期是先打开该文件以便没有人能从下删除该文件。我认为这解释了为什么方法的行为像它。该框架设计师们被困在岩石与险境间。如果他们能够第一次打开文件时,他们会冒着其他程序轰炸上的文件共享错误。如果他们不先打开该文件后,随机和偶发风险程序轰炸。非常难以诊断。有两个不愉快的选项之间进行选择,他们选择了不弹什么的人。



安美居,使其可靠通过打开文件。


Please, explain your thoughts.

1.  DateTime dt = System.IO.File.GetLastAccessTime("C:\\There_is_no_such_file.txt");
2.  DateTime dt = System.IO.File.GetLastAccessTime("");

  1. If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

  2. In the second situation argument exception is thrown.

Why in first case FileNotFoundException (or smth. simmilar) is not thrown?

解决方案

This is documented behavior. From the Remarks section in the MSDN Library topic:

If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

The exception you get when you pass an empty string is one that's generated by code that checks if the passed string is a valid path name. Which is fair, that would be bug in the program.

The code is explicit so it wasn't done by oversight or by mistake. It uses the FindFirstFile() API function to locate the file. If that fails, it checks the Windows error. And explicitly ignores, the "File not found", "Path not found" and "Drive busy" errors.

Beware that offered solutions that use File.Exists don't actually prevent this problem. Windows is a multi-tasking operating system. Your thread may be pre-empted right after the Exists call and another process may delete the file. When your thread regains the CPU, you'll still get the bogus date.

The only guaranteed way to get an accurate date is to open the file first so that nobody can delete the file from under you. Which I think explains why the method behaves like it does. The framework designers were stuck between a rock and a hard place. If they would have opened the file first, they would have risked other programs bombing on a file sharing error. If they don't open the file first, they risk your program bombing randomly and infrequently. Extremely hard to diagnose. Having to choose between two unpleasant options, they chose the one that doesn't bomb anything.

Anyhoo, make it reliable by opening the file.

这篇关于C# - 为什么不System.IO.File.GetLastAccessTime返回一个预期值时,未找到该文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆