各位:
 
.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. Js的new运算符

    new运算符 在JavaScript中,new是一个语法糖,可以简化代码的编写,可以批量创建对象实例. 语法糖Syntactic sugar,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影 ...

  2. Django配置站点

    一 修改hosts文件 """ hosts文件涉及的dns解析 1.找到hosts文件 windows: C:\Windows\System32\drivers\etc\ ...

  3. hdu3973 AC's String 线段树+字符串hash

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3973/ 题意是:给出一个模式串,再给出一些串组成一个集合,操作分为两种,一种是替换模式串中的一个字符,还有一种是 ...

  4. C#中的9个“黑魔法”与“骚操作”

    C#中的9个"黑魔法"与"骚操作" 我们知道C#是非常先进的语言,因为是它很有远见的"语法糖".这些"语法糖"有时过于好 ...

  5. pikachu学习-暴力破解模块

    安装好XAMPP,burpsuite,配置好pikachu我们就可以进行pikachu平台的漏洞学习 我这篇博客主要写暴力破解模块讲解,它分为4个小模块,分别是“基于表单的暴力破解”,“验证码绕过(o ...

  6. Building Applications with Force.com and VisualForce (DEV401) (二十):Visualforce Pages: Visualforce Componets (Tags)

    Dev401-021:Visualforce Pages: Visualforce Componets (Tags) Module Agenda1.Tag Basics2.Tag Bindings T ...

  7. 在TensorFlow中实现文本分类的卷积神经网络

    在TensorFlow中实现文本分类的卷积神经网络 Github提供了完整的代码: https://github.com/dennybritz/cnn-text-classification-tf 在 ...

  8. 谈谈flex布局实现水平垂直居中

    我们在这要谈的是用flex布局来实现水平和垂直居中.随着移动互联网的发展,对于网页布局来说要求越来越高,而传统的布局方案对于实现特殊布局非常不方便,比如垂直居中.所以09年,W3C 提出了一种新的方案 ...

  9. Hadoop调试记录(1)

    错误 ERROR: Can't get master address from ZooKeeper; znode data == null 解决 关闭hadoop,发现stop-all.sh后几个进程 ...

  10. Java程序员拥有这些工具,还怕干不出好活

    老话说,工欲善其事,必先利其器. 那么,作为编程人员,你都用过哪些“兵器”呢?你的”兵器“够”锋利”吗? 近期,有很多新朋友问,都有什么画流程图的工具,给推荐推荐? 索性,就静下来,好好梳理一下,从事 ...