Ryan Michela
Ryan Michela

Reputation: 8374

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.

Upvotes: 5

Views: 358

Answers (2)

the_mandrill
the_mandrill

Reputation: 30842

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.

Upvotes: 1

Chris Thornton
Chris Thornton

Reputation: 15817

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

Upvotes: 6

Related Questions