PRB: My application can't write to its directory when run as a regular user
Article Created: 2003-10-25 19:05 GMT by Jordan Russell
Last Updated: 2004-04-10 05:17 GMT by Jordan Russell
On Windows 2000 and XP, the security on the Program Files directory grants only read access to members of the Users group. A user must be a member of the Power Users or Administrators group in order to write to the directory (and by default, any subdirectories too).
How can I work around this?
If your application needs to store configuration files, they should be placed under a directory like Application Data instead, which is guaranteed to be writable by the user. (You can get the path of Application Data by calling SHGetFolderPath from shfolder.dll or SHGetSpecialFolderLocation.)
Alternatively, configuration settings can be stored in the registry under HKEY_CURRENT_USER\Software, which is also guaranteed to be writable by the user.
Couldn't I just change the security on my application's directory?
You could, but it opens up a security hole. Granting write access on your application's directory to the Users group, or Everyone, potentially makes it possible for anyone on the system to attain elevated privileges -- even adminstrative privileges. How so?
If everyone is allowed to write to the directory, then a malicious user could replace your program's EXE with a trojan of their choice. This trojan would then be unleashed the next time a user logs onto the system and tries to run your program. If that user happens to have administrative privileges, then the trojan will have complete control of the system.
Granted, as an application developer you may not be overly concerned about such potential security exploits, but bear in mind that the system administrator at your customer's workplace very well may be, otherwise he would have given users administrative or Power User privileges to begin with.
With that said, it is usually safe to change the security on a subdirectory of your application's directory which contains only data and no program files (e.g. C:\Program Files\My Program\Data). For more information on doing this, see Changing Permissions in the FAQ.
Site contents Copyright © 1997-2012 Jordan Russell. All rights reserved.