Reputation: 520
I have a program that needs to create a text log. In XP, everything worked just fine, but when I tested it on Windows 7 - and was later Upgraded to Windows 7 for my Dev Computer - I get an error that "Access to the path 'C:\Program Files (x86)\Ludlum Measurements, Inc\LMI Universal Software\UniversalLog\' is denied". I get the a similar error if I have the installer create the folder and then try to create a text file in the folder.
I know this has something to do with permissions, but what should I give permission and how?
The programs that use the logging function?
The folder that the logs go into?
The folder the programs are in?
And how would this fix work under XP, Vista, Server 2005, and Server 2008 and as service (Part of the program runs as a Windows form, and part as a service)?
-Edit- Emphasis added.
To reiterate: How will this work when installed and ran on an XP? A Server 2003? A Server 2008? Will this cause any problems for them?
And a new question: Can my program access files from the ApplicationData file? I just remembered the entire problem stemmed from me trying to create a config file that holds connection information for to an SQL database. Hitting your head against a brick wall for a day tends to make you forget why you started doing it in the first place. And again, this originally worked in XP, Server 2005, and Server 2008.
Upvotes: 3
Views: 1676
Reputation: 19543
You shouldn't write anything to the Program Files directory after installation unless you are actually patching your application. Day-to-day usage of files should go into the application data folder (LocalApplicationData for non-roaming data and ApplicationData for roaming data).
string path = Environment.GetFolderPath(
EnvironmentFolders.SpecialFolders.LocalApplicationData)
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
Upvotes: 4
Reputation: 24403
You should use Environment.GetFolderPath and pick a Environment.SpecialFolder folder that has write permissions when the UAC is on and write your log files there.
By default under UAC you donot have permissions to write to C:\Program Files or its subfolders.
Environment.ApplicationData is usually a good choice, it will be writable by default on XP , Vista , Server 2005 and Server 2008 unless the admin has locked stuff down too much
Teach Your Apps To Play Nicely With Windows Vista User Account Control
is a good resource on how to write an application that behaves nicely under UAC
Upvotes: 4