各位:
.NET Framework 本省在设计的时候,他对于异常没有彻底作到抛出,这样可能会有不少意想不到的问题。
好比
这个文件在资源管理器中能够访问,可是在你的应用程序中通常不能访问。这个时候File.Exists 会返回false,其实文件时存在的。
缘由很简单,ASP.NET 默认是本机的 ASPNET 用户,这个用户没有访问权限。
咱们看一下File.Exists 的内部实现,(System.IO.FIle 类放在mocorlib.dll 里面,各位能够用相似reflector 之类的工具反编译一下)
public static bool Exists(string path){ string[] textArray1; try { if (path == null) { return false; } if (path.Length == 0) { return false; } path = Path.GetFullPathInternal(path); textArray1 = new string[1]; textArray1[0] = path; new FileIOPermission(1, textArray1, 0, 0).Demand(); //显式的要求一个权限 return File.InternalExists(path); } catch (ArgumentException) { } catch (NotSupportedException) { } catch (SecurityException) //出现异常以后并无抛出 { } catch (IOException) { } catch (UnauthorizedAccessException) { } return false; //这就是你看到的false}