FileIOPermission issue trying to ReadAllBytes

altec1974
altec1974 used Ask the Experts™
on
I'm trying to ReadAllBytes into a Byte[] contents variable but I get a permissions issue.  Take a look at the code.  

I get this exception:

"Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."

Can someone please tell me what I'm missing or what I need to code to get rid of this exception and read the file into the variable?

I have tried setting the permissions with the FileIOPermission but still no luck )o:

Thanks,
Roberto
Byte[] contents = null;
            try
            {
                string FullFileName = attach.Path + "\\" + attach.FileName;
                // FILE PERMISSIONS TO UPLOAD
 
                FileIOPermission fileIOPerm1;
                
                fileIOPerm1 = new FileIOPermission(FileIOPermissionAccess.AllAccess, FullFileName);
                fileIOPerm1.AllFiles = FileIOPermissionAccess.Read;
                fileIOPerm1.AllLocalFiles = FileIOPermissionAccess.AllAccess;
                fileIOPerm1.SetPathList(FileIOPermissionAccess.Read, attach.Path);
 
                string[] paths = fileIOPerm1.GetPathList(FileIOPermissionAccess.Read);
 
                contents = File.ReadAllBytes(FullFileName);
            }
            catch (Exception ex)
            {
                CommonUtils.Log(CommonUtils.ERROR, "Cannot read File from physical path: " + attach.Path + "\n" + ex.Message + "\n" + ex.StackTrace);
            }
            return contents;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
By the way, I'm using the .net Framework 2.0.

Commented:
Try to execute following and check that it is able to read content.

Byte[] contents = null;
            try
            {
                string FullFileName = attach.Path + "\\" + attach.FileName; 
 
                contents = File.ReadAllBytes(FullFileName);
            }
            catch (Exception ex)
            {
                CommonUtils.Log(CommonUtils.ERROR, "Cannot read File from physical path: " + attach.Path + "\n" + ex.Message + "\n" + ex.StackTrace);
            }
            return contents;

Open in new window

And if that doesn't fix your problem, or if you NEED to use FileIOPermission, tell more about your application. Are you trying to access files across a network? If not where is the file? Are you using a ClickOnce deployment?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
I get the same error when trying to run the code you sent, Jinal:

Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Author

Commented:
Hi dericstone,

The application is like a web outlook in which you need to be able to attach files, but what you do is upload them to the server first, and then send them.  The problem is that whenever I ReadAllBytes I get the permissions exception...
So is your problem with the upload part or the sending part? If with the sending part, I would guess that the upload part is not properly closing the files and releasing the handles. If with the upload part, is there a prior step that is not no properly releasing the files?

Author

Commented:
How can I release a file, that I cannot even read?
You need to answer these questions for me to help:
1) Are you accessing files across a network?
2) When you get this error can you open the same file in a text editor or other proven application logged in as the same user?
3) What application is creating the files, did you create that application, and in what state is that application when the error occurs?
4) Are you using a ClickOnce deployment?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial