I have code like the following to test for existence of a file. I know thefile is there, but File.Exists returns FALSE. The problem appears to bethat the file is in a directory beneath "My Documents". When I move it to adirectory directly under the root, File.Exists returns TRUE. So I triedusing FileIOPermission to give me rights to read it, but still no luck. Anysuggestions? Thanks...FileIOPermission f = newFileIOPermission(FileIOPermissionAccess.AllAccess, fileName);if (!File.Exists(fileName))throw new FileNotFoundException("Unable to constructBinaryUploadFile object. File does not exist:\n\n" + fileName + "\n\n"); 解决方案 Dan,If you don''t have access to the directory, then Exists will returnfalse, 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 indicatewhether or not you have permission. Between the call to Exists, and theconstruction of the permission, call Demand on the permission. It willthrow a SecurityException if you don''t have access to the directory.Hope this helps.--- Nicholas Paldino [.NET/C# MVP]- mv*@spam.guard.caspershouse.com"Dan" <da*@dontspamme.com> wrote in messagenews: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 returnfalse, 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 indicatewhether or not you have permission. Between the call to Exists, and theconstruction of the permission, call Demand on the permission. It willthrow a SecurityException if you don''t have access to the directory.Hope this helps.--- Nicholas Paldino [.NET/C# MVP]- mv*@spam.guard.caspershouse.com"Dan" <da*@dontspamme.com> wrote in messagenews: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 rightpermissions 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 thecaller.You are confusing this security mechanism with .NET''s "Code access security", this kind of security mechanism is based on the "code identity" (where didthe code came from - internet, intranet, codegroup , ...) NOT the windowsuser identity, these are fundamentally different. Whatever you do in code(that is Code Access Security) won''t help if the "windows user" has noaccess rights for the objects (like files) controlled by the OS securitysystem (Windows security).In short you need to fix the NTFS security settings on the file/folder.Willy."Dan" <da*@dontspamme.com> wrote in messagenews: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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!