File.Exists问题

编程入门 行业动态 更新时间:2024-10-27 06:21:54
本文介绍了File.Exists问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码来测试文件是否存在。我知道 文件在那里,但File.Exists返回FALSE。问题似乎是 ,该文件位于我的文档下面的目录中。当我将它移动到根目录下的 目录时,File.Exists返回TRUE。所以我尝试使用FileIOPermission给我一个读取它的权利,但仍然没有运气。任何 的建议?谢谢...... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess,fileName); if(!File。存在(fileName)) 抛出新的FileNotFoundException(无法构造 BinaryUploadFile对象。文件不存在:\ n \ nn+ + fileName +" \ n \\ nn; \\ n;

解决方案

Dan, 如果你不有权访问该目录,然后Exists将返回 false,如您所料。 您正在创建权限以查看您是否可以访问该目录, 以防止误报。但是,无论您是否获得许可,您都无法表明 。在对Exists的调用和 构造的权限之间,请求权限请求。如果你无法访问该目录,它将会抛出一个SecurityException。 希望这会有所帮助。 - - - Nicholas Paldino [.NET / C#MVP] - mv * @ spam.guard.caspershouse " Dan" < da*@dontspamme>在消息中写道 新闻:Os ************** @ TK2MSFTNGP15.phx.gbl ...

我的代码类似于以下测试是否存在文件。我知道文件在那里,但File.Exists返回FALSE。问题似乎是该文件位于我的文档下面的目录中。当我将它移动到根目录下的目录时,File.Exists返回TRUE。所以我尝试使用FileIOPermission给我权利阅读它,但仍然没有运气。任何建议?谢谢...... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess,fileName); if(!File.Exists(fileName))抛出新的FileNotFoundException (无法构造 BinaryUploadFile对象。文件不存在:\ n \ n" + fileName +" \ n \ n");

Dan, 如果您无法访问该目录,则Exists将返回 假,如你所料。 您正在创建权限,以查看您是否可以访问目录, 以防止误报。但是,无论您是否获得许可,您都无法表明 。在对Exists的调用和 构造的权限之间,请求权限请求。如果你无法访问该目录,它将会抛出一个SecurityException。 希望这会有所帮助。 - - - Nicholas Paldino [.NET / C#MVP] - mv * @ spam.guard.caspershouse " Dan" < da*@dontspamme>在消息中写道 新闻:Os ************** @ TK2MSFTNGP15.phx.gbl ...

我的代码类似于以下测试是否存在文件。我知道文件在那里,但File.Exists返回FALSE。问题似乎是该文件位于我的文档下面的目录中。当我将它移动到根目录下的目录时,File.Exists返回TRUE。所以我尝试使用FileIOPermission给我权利阅读它,但仍然没有运气。任何建议?谢谢...... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess,fileName); if(!File.Exists(fileName))抛出新的FileNotFoundException (无法构造 BinaryUploadFile对象。文件不存在:\ n \ n" + fileName +" \ n \ n");

如果您(运行代码的Windows标识)对保存文件的文件夹没有正确的 权限,File.Exists将返回false, 正如您所遇到的那样。 这称为windows access security。基于 来电者的身份。 您将这种安全机制与.NET的代码访问安全性混淆了 ,这种安全机制是基于代码身份的。 (代码来自哪里?代码来自 - 互联网,内联网,代码组......)不是windows 用户身份,这些根本不同。无论你在代码中做什么 (即代码访问安全性)如果windows用户不会有帮助。对操作系统安全控制的对象(如文件)没有 访问权限 系统(Windows安全)。 简而言之,您需要修复文件/文件夹上的NTFS安全设置。 Willy。 Dan < da*@dontspamme>在消息中写道 新闻:Os ************** @ TK2MSFTNGP15.phx.gbl ...

我的代码类似于以下测试是否存在文件。我知道文件在那里,但File.Exists返回FALSE。问题似乎是该文件位于我的文档下面的目录中。当我将它移动到根目录下的目录时,File.Exists返回TRUE。所以我尝试使用FileIOPermission给我权利阅读它,但仍然没有运气。任何建议?谢谢...... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess,fileName); if(!File.Exists(fileName))抛出新的FileNotFoundException (无法构造 BinaryUploadFile对象。文件不存在:\ n \ n" + fileName +" \ n \ n");

I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My Documents". When I move it to a directory directly under the root, File.Exists returns TRUE. So I tried using FileIOPermission to give me rights to read it, but still no luck. Any suggestions? Thanks... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileName); if (!File.Exists(fileName)) throw new FileNotFoundException("Unable to construct BinaryUploadFile object. File does not exist:\n\n" + fileName + "\n\n");

解决方案

Dan, If you don''t have access to the directory, then Exists will return false, as you expected. You are creating the permission to see if you can access the directory, to prevent a false negative. However, you aren''t doing anything to indicate whether or not you have permission. Between the call to Exists, and the construction of the permission, call Demand on the permission. It will throw a SecurityException if you don''t have access to the directory. Hope this helps. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse "Dan" <da*@dontspamme> wrote in message news:Os**************@TK2MSFTNGP15.phx.gbl...

I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My Documents". When I move it to a directory directly under the root, File.Exists returns TRUE. So I tried using FileIOPermission to give me rights to read it, but still no luck. Any suggestions? Thanks... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileName); if (!File.Exists(fileName)) throw new FileNotFoundException("Unable to construct BinaryUploadFile object. File does not exist:\n\n" + fileName + "\n\n");

Dan, If you don''t have access to the directory, then Exists will return false, as you expected. You are creating the permission to see if you can access the directory, to prevent a false negative. However, you aren''t doing anything to indicate whether or not you have permission. Between the call to Exists, and the construction of the permission, call Demand on the permission. It will throw a SecurityException if you don''t have access to the directory. Hope this helps. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse "Dan" <da*@dontspamme> wrote in message news:Os**************@TK2MSFTNGP15.phx.gbl...

I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My Documents". When I move it to a directory directly under the root, File.Exists returns TRUE. So I tried using FileIOPermission to give me rights to read it, but still no luck. Any suggestions? Thanks... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileName); if (!File.Exists(fileName)) throw new FileNotFoundException("Unable to construct BinaryUploadFile object. File does not exist:\n\n" + fileName + "\n\n");

If you (the windows identity running the code) don''t have the right permissions to the folder holding the file, File.Exists will return false, as you are experiencing. This is called "windows access security " based on the identity of the caller. You are confusing this security mechanism with .NET''s "Code access security" , this kind of security mechanism is based on the "code identity" (where did the code came from - internet, intranet, codegroup , ...) NOT the windows user identity, these are fundamentally different. Whatever you do in code (that is Code Access Security) won''t help if the "windows user" has no access rights for the objects (like files) controlled by the OS security system (Windows security). In short you need to fix the NTFS security settings on the file/folder. Willy. "Dan" <da*@dontspamme> wrote in message news:Os**************@TK2MSFTNGP15.phx.gbl...

I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My Documents". When I move it to a directory directly under the root, File.Exists returns TRUE. So I tried using FileIOPermission to give me rights to read it, but still no luck. Any suggestions? Thanks... FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.AllAccess, fileName); if (!File.Exists(fileName)) throw new FileNotFoundException("Unable to construct BinaryUploadFile object. File does not exist:\n\n" + fileName + "\n\n");

更多推荐

File.Exists问题

本文发布于:2023-06-08 14:29:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/583081.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:File   Exists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!