Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is a safe place to store log files that is compatible with XP, Vista, and Windows 7?

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.

like image 849
Ryan Michela Avatar asked May 11 '10 20:05

Ryan Michela


1 Answers

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...

like image 183
Chris Thornton Avatar answered Dec 02 '22 01:12

Chris Thornton