zoukankan      html  css  js  c++  java
  • 关于 System.IO.File.Exists 需要注意的事项

    各位:
     
    .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
    }
  • 相关阅读:
    C# 单点登录
    长度12的数组,要求对数据分为3组,每组数据对应位置的数字为前几位的和,并返回12位数组
    react项目初始化
    vue 过滤器的使用(解决forEach遇到的问题)
    nuxt中less使用
    vue项目less 使用
    Webpack中的sourceMap配置
    webpack 同一文件打包两次生成两个文件
    webpack---图片打包前和打包后名称一致的配置
    The computed property "userName" is already defined in data.
  • 原文地址:https://www.cnblogs.com/qi123/p/9336819.html
Copyright © 2011-2022 走看看