I have a winforms app that needs to write out logs. The app also needs to run without UAC warnings and using a non-admin account.
Where can I write my logs without running afoul of the default security settings? In addition, this has to work on Windows XP, Vista, and 7.
Extra points if all users can write to the same set of log files.
You cannot (should not) hard-code the path. Instead, ask Windows where to store it by calling SHGetFolderPath, which can be used to return the location of the current user's (or all users) AppData directory.
MSDN article, with C solution: http://support.microsoft.com/kb/310294
Useful Delphi example: http://delphi.about.com/od/kbwinshell/a/SHGetFolderPath.htm
Note that it is hazardous to do a one-time query for the path, convert to an absolute string, and store that in your settings. Because Windows can change it. They can join a domain, upgrade windows, etc., and windows may indeed move your files. If you then point at the old location, without asking again "where is this folder", you'll break. Been there, done that...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With