views:

67

answers:

2

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.

+6  A: 

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

Chris Thornton
+1  A: 

Using SHGetFolderPath you have the option of using per-user or global log file directories. If you use CSIDL_COMMON_APPDATA then: "This information will not roam and is available to anyone using the computer." which sounds like it's what you want.

the_mandrill