File.Copy to Program Files Directory. RRS feed

  • Question

  • I searched high and low and have found some discussions on this topic but do seem to see any solutions.   So here is my issue.   I have an application that will copy 2 files to the Program Files directory.   What happen is, when I run it and the files exist (this should be what normally happens) I get accessed denied.   If I try to do a File.Delete (in code) first I get an access denied, however I can manually (in OS windows explorer) delete the files and then the copy will work.   I am running the application as Admin, so permissions to the folder are good.  

    The application is restoring a 3rd party software file that tends to get corrupted a lot.   We have mechanism that will back this file up, but when I try to restore it I get the problem described above.   (not a file lock issue, I have made sure no programs have a handle to the file)

    OS = Vista32
    Program written in C#

    Is there anyway of accomplishing this?

    Tuesday, February 16, 2010 2:46 PM


All replies

  • I'm not entirely sure if this is the solution, but, perhaps it can give you an idea.

    Requirements in vista and windows 7 state that the manifest must declare the security permissions (the level) required by the application. The program files directory is protected by UAC and can only be written to if the application starts in Administrative mode. I understand you mentioned that the application is being "run" as admin, but, is the manifest stating that the application requires admin security? Perhaps since the manifest does not say it, the elevated process is not being truly elevated.

      I apologize that it's a blind guess, but that's all I have.
    Tuesday, February 16, 2010 6:21 PM
  • Hi,

    Please make sure that the path you passed to File.Delete method is NOT a directory. And, we will get an access-denied exception if the target file is read-only.

    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by jbuzz Wednesday, February 17, 2010 2:48 PM
    Wednesday, February 17, 2010 3:03 AM
  • This is chiefly a Vista and Windows 7 issue.   Make sure you're running your application under elevated privileges.  If you're copying something to the Program files directory, you're most likely performing an install.  Use a Setup and Deployment project for that.  It'll make the process much easier.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Wednesday, February 17, 2010 3:20 AM
  • David its not an install more like a fix or restore.   We have a db file created by 3rd party software that likes to get corrupted.   So what we do is backup the file and now need a way for a help desk person to restore it.   That is where the application come into play you know 1 click solution.  I would like to move it out of program files, but I do not have that kind of pull.    

    I am passing the filename to the File.Delete command and it is not marked as read-only.

    I have been playing around with the manifest but have not got it to work yet.   But this seems like the way to make it work.   I do require that the application runs as an admin user.

    Wednesday, February 17, 2010 2:08 PM
  • Yeah, the manifest is the way to go here.  Admin or not, your UAC privilege needs to be elevated.  The manifest helps you do that.  I'd keep fiddling with it (and reading some good tutorials and such) and I bet that will solve your issue.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Wednesday, February 17, 2010 2:15 PM
  • Ok all here is what I found.

    Even though I can check the file via Windows Explorer and see that it is not readonly, to the program it is. 

    So in code if I checked to see if its readonly then remove the flag I could copy the file.
    if ((File.GetAttributes(toDb) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        File.SetAttributes(toDb, FileAttributes.Normal); 
    File.Copy(dbFile, toDb, true);
    thanks everyone for the help.

    Wednesday, February 17, 2010 2:52 PM