views:

455

answers:

2

I found out an application I wrote does not work properly under Windows Vista/7 if UAC is enabled at any level, because it writes files to the install directory of the program, defaults to "C:\Program Files\MyProgram." If UAC is disabled (or on any other version of Windows) it works properly - I read that UAC denies applications write access to the Program Files directory by default.

My question is, well, how should I write my application so that it can be used without any "rights" needed at all. I don't want users to have to run it with elevated privileges or as administrator. I just want it to work. Are there certain directories that any app has write access to under UAC where it might be better to write my files? They are mostly config files that are dynamically created/destroyed/updated.

Thanks for you help!

+10  A: 

Per-user application specific data should be written in the AppData folder.

You should use SHGetKnownFolderPath with FOLDERID_LocalAppData.

In managed code, you should use System.Environment.GetFolderPath with System.Environment.SpecialFolder.LocalApplicationData.

Michael
If you use the .NET configuration abilities (`System.Configuration` namespace, these are the .settings files in a C# project), your data will be placed in the proper location and you'll have very elegant ways to access and update it.
280Z28
+4  A: 

Yes, there are specific locations. Consider this msdn article as a first reference. It mentions the locations:

  • CSIDL_APPDATA
  • CSIDL_LOCAL_APPDATA
  • CSIDL_COMMON_APPDATA

In native code, the method SHGetKnownFolderPath should prove useful.

In managed code you can use Environment.GetFolderPath(). If you're in a specific application framework, such as windows forms, you can get even easier access via direct properties, such as Application.LocalUserAppDataPath (which is my personal favorite technique). The framework path will include app-specific qualifiers on the path it returns to distinguish between (e.g.) different versions of your app.

Greg D