各位:
 
.NET Framework 本省在设计的时候,他对于异常没有完全做到抛出,这样可能会有很多意想不到的问题。
 
比如
你在asp.net 应用程序中判断文件是否存在,这个文件可能是一个共享路径 ,比如: System.IO.File.Exists(//montaquehou-mis/share/a.file)
这个文件在资源管理器中可以访问,但是在你的应用程序中一般不能访问。这个时候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
}

关于 System.IO.File.Exists 需要注意的事项的更多相关文章

  1. IIS目录下文件共享后System.IO.File.Exists返回false

    场景:在iis目录下,因为特殊需要共享一个文件夹,给到其他的技术人员访问,突然发现小小的操作,搞“大”了,使用 string path = Server.MapPath("~/file/te ...

  2. 详解C#中System.IO.File类和System.IO.FileInfo类的用法

    System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...

  3. System.IO.File类和System.IO.FileInfo类

    1.System.IO.File类 ※文件create, copy,move,SetAttributes,open,exists ※由于File.Create方法默认向所有用户授予对新文件的完全读写. ...

  4. System.IO.File.Create 不会自动释放,一定要Dispose

    这样会导致W3P进程一直占用这个文件 System.IO.File.Create(HttpContext.Current.Server.MapPath(strName)) 最好加上Dispose Sy ...

  5. System.IO.File.WriteAllText("log.txt", "dddd");

    System.IO.File.WriteAllText("log.txt", "dddd");

  6. C# System.IO.File

    using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp ...

  7. system.io.file创建

    在实际开发中,如果用的文件名不能确定位置.或名字.可以使用GUID类来命名函数.Guid 结构标识全局唯一标示符.其NewGuid结构可以初始化一个新历.该方法语法格式如下: public stati ...

  8. 未处理的异常:system.io.file load exception:无法加载文件或程序集“ 。。。。 找到的程序集的清单定义与程序集引用不匹配。

    问题描述: 添加控制器的时候,突然就报了这个错: Unhandled Exception: System.IO.FileLoadException: Could not load file or as ...

  9. 安卓上为什么不能用system.io.file读取streammingAssets目录下的文件

    首先,看文档: Streaming Assets   Most assets in Unity are combined into the project when it is built. Howe ...

随机推荐

  1. wxPython4.0.4关于我们

    源码地址:https://download.csdn.net/download/zy0412326/12154342 wxPython4改版后将AboutBox方特到adv包里面.如果想快速实现GUI ...

  2. [贪心]Codeforces Equal Rectangles

    Equal Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. MySQL优化之慢查询日志

    慢查询日志概述 所谓慢查询日志,就是用于记录MySQL中响应时间超过设定阈值的SQL语句,通过打开慢查询开关,MySQL会将大于阈值的SQL记录在日志中,以便于分析性能. 慢查询日志选项默认是关闭的, ...

  4. Building Applications with Force.com and VisualForce (DEV401) (二) : Application Essentials:Designing Application on the Force.com Platform

    Dev 401-002:Application Essentials:Designing Application on the Force.com Platform Course Objectives ...

  5. python之线程和进程

    进程(process)和线程(thread)是操作系统的基本概念,但是它们比较抽象,不容易掌握.最近,我读到一篇材料,发现有一个很好的类比,可以把它们解释地清晰易懂. 1:计算机的核心是CPU,它承担 ...

  6. TensorBoard中HISTOGRAMS和DISTRIBUTIONS图形的含义

    前言 之前我都是用TensorBoard记录训练过程中的Loss.mAP等标量,很容易就知道TensorBoard里的SCALARS(标量)(其中横纵轴的含义.Smoothing等). 最近在尝试模型 ...

  7. 报错代码:svn-http status413'requset entity too large

    报错代码:svn-http status413'requset entity too large 发现报错,判断问题.解决问题.记录问题. SVN服务器端排查过没有问题,其他客户端都能正常更新.只有一 ...

  8. windows找不到文件gpedit.msc处理方法

    新建一个txt,输入 @echo offpushd "%~dp0"dir /b C:\Windows\servicing\Packages\Microsoft-Windows-Gr ...

  9. Python python对象 range

    """ range(stop) -> range object range(start, stop[, step]) -> range object Retu ...

  10. MATLAB—地图

    一.画亚洲地图 1.worldmap() (1) clear all worldmap('World') clear all worldmap('World')%世界地图 load coast %载入 ...