.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
关于 System.IO.File.Exists 需要注意的事项

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

各位:
 
.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
}



相关文章

相关软件