UnauthorizedAccessException with System.IO.File.InternalCopy in ProgramData on Windows 7
-
04-10-2020 - |
سؤال
I have a program installed in ProgramData. I use UAB 2.1 to updating app but it's failed because accessing to the path is denied (OK with text files so I think there's problem with UAC). How can I get overwriting rights in ProgramData/MyAppFolder or display a dialog to notice user give overwriting rights for updater.
Many thanks in advance.
Update
I've read an article at http://blogs.msdn.com/b/cjacks/archive/2008/02/05/where-should-i-write-program-data-instead-of-program-files.aspx. It said that You'll want to create your directory and set the ACLs you need at install time. Should I change installation folder to LocalAppData or try to set ACLs at install time according the article.
المحلول
Apps that update themselves have a hard time under UAC. Your best bet is to partition the code that does the update into a separate exe. Put a manifest with requireAdministrator on that exe. In the main app, leave the code that checks for updates, and have it enable a button or menu item when an update is needed. Put a shield on that button or menu item, and have it launch the separate exe with ShellExecute. The user will get a UAC consent dialog, which they will be expecting because of the shield. The separate exe, because it's elevated, will be able to write to Program Files and other protected areas.