My Winform application uses xml files to store data, where can I store them so that Vista users can write to it?

advertisements

My winform app uses xml files to store data, where should I store them so Vista users can write to them?

Thanks


Use the Environment.GetFolderPath to get the most appropriate folder in an OS-independent manner.

In particular, you need one of the following SpecialFolder values:

  • ApplicationData - if the files are roaming, per-user and are for the application use only and don't represent documents the user might care about.
  • LocalApplicationData - if the files are non-roaming, per-user and are for the application use only and don't represent documents the user might care about.
  • CommonApplicationData - if the files are roaming, common for all user and are for the application use only and don't represent documents the user might care about. NOTE: On Vista this maps to C:\ProgramData, which is by default read-only for regular users (due to the fact that changing files in there might affect behavior of programs used by admins). You can either explicitly change the permissions on your app subfolder, or choose one of the other options.
  • MyDocuments - if the files are per-user and represent documents.

Note that there's no SpecialFolder enum value like CommonDocuments that would represent machine-wide document store, even though there is a folder intended to serve like one (C:\Documents and Settings\All Users\Documents on XP and C:\Users\Public\Documents on Vista). You will have to find the OS version yourself and choose the appropriate folder if you want to write to these locations.

Internally Environment.GetFolderPath uses the Win32 API SHGetFolderPath. The enumeration used by SHGetFolderPath gives you the well-known locations for several other special folders (including Common Documents). You might use directly SHGetFolderPath; you can find it's p/invoke definition and the corresponding CSIDL enum definition on PInvoke.net.

You can also use the IsolatedStorage. However, it is non-roamable, per-user, with limited quota and is not easily accessible to the user from Windows Explorer. As such, it is really a medium/low-trust equivalent to SpecialFolder.ApplicationData.