views:

458

answers:

1

Simple question really...

How can I copy a ReadOnly file?

When running a debug build of my very simple Windows Forms app, I'm getting the UnauthoriazedAccess Exception when trying to copy a read-only file from one directory to another:

FileInfo newFile = new FileInfo(fileName);
File.Copy(newFile.FullName, Path.Combine(destinationDirPath, newFile.Name), true);

What do I need to do to get around it? I'm thinking that there's some kind of security or application permission that I need to set for this project...

+1  A: 

Is it possible that you're getting the exception not for reading the file but for the place you are writing the file? If that's the case you need to make sure you're writing the file to a directory for which the application credentials have access.

JaredPar
You're right. The folder created as the result of Add Folder in the FolderBrowserDialog window was read only. I guess that's the source of the problem.
Valentein