I've built a Windows Application using c#, on Windows 7.
Everything was working fine, so I've created a Setup Wizard project and then built it. Once I install the app, I can open it correctly, but when I try to make some action that writes a text file(with logging purposes) it cra开发者_JS百科shes, thrwoing me the following error message:
UnauthorizedAccessException
Access to the path 'C:\Program Files (x86)\MSProgram\MSProgram\log.txt' is denied.
When I manually give that folder full rights, it works fine. Now, the question is the following:
How do I programmatically give the app rights for writing things in my app's directory? So every person that downloads it doesn't experience the same problem.
Don't. Applications should not write data into their installation directory directly. Doing so will make the application work poorly on Windows Vista and Windows 7, since it's not the proper way of saving data.
You should instead use Environment.GetFolderPath, and write into a good location, such as the user's application data folder (Environment.SpecialFolders.ApplicationData).
The solution is not to grant rights to that directry but to instead write to a folder that is more suitable for application logs. The "Program Files(x86)" and "Program Files" is a place for application installation, not logging.
A more appropriate location would be the per user data folders
- %AppData%
- %LocalAppData%
Or the result of Environment.GetFolderPath
for the following values
- SpecialFolder.ApplicationData
- SpecialFolder.LocalApplicationData
- SpecialFolder.CommonApplicationData
Generally it's not good practise to write to the Program Files directories, I usually write log file to the AppData folder, which you can get at by using:
var logFilename = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "log.txt")
You will probably want to create a directory in there or give the log file a more descriptive name. You may also want to consider if multiple instances of your app could be running for the same user.
If you must write to the Program Files directory you will need either run the application with administrator privileges, either using run as administrator, or by requesting higher privileges on your application. Another possibility would be to setup your installer to give the installing user full folder rights, although how to do this will depend on the installer you are using.
Hope this helps.
Andy
To add to what Reed Copsey and JaredPar wrote in their answers:
Writing to the "Program Files" or "Program Files (x86)" directories is not advisable, and for good reason. The reason for this is that granting permissions for any application to write and/or change files in these directories is a security problem - it allows program that are "jeopardized" (e.g. your browser, in case it has a security bug) to alter programs files, infecting them with viruses or worms, etc.
Windows has dedicated folders for application and user data, and any program accessing data in these folders is expected to treat it as "untrusted data" due to the lower security restrictions on those folders. One such folder is Environment.SpecialFolders.ApplicationData - as indicated by Reed.
精彩评论