5

On the local system, is there a (reasonably) guaranteed place to create a directory structure to write files? I'm looking for a solution that does not require Administrative Privileges for permission changes?

After a bit of research, I've seen this answer for finding the current user's desktop folder. I could use this, but what other options are available?

Putting together research:

System.Environment.SpecialFolder Enumeration

Community
  • 1
  • 1
Christopher Stevenson
  • 2,843
  • 20
  • 25
  • 2
    The executing directory is always guaranteed. – Botonomous Sep 25 '13 at 19:25
  • 4
    Really? The executing application can always write to the directory it is started in? Can you provide a reference? I don't believe that is true (remember with Vista, programs that were used to writing to folders under their installation location no longer functioned unless granted elevated privileges, or if UAC was disabled). – Eric J. Sep 25 '13 at 19:31
  • I was amusing only read permissions not write (guess I missed it in the question). Yes of course you are correct if wanting write permissions. – Botonomous Sep 25 '13 at 21:06
  • @EricJ.: In Vista, programs that were used to writing folders under their installation location usually still functioned even if not granted elevated privileges. However, this was due to a compatibility shim which caused the read/write operations to be redirected to a different folder. Of course, this has all sorts of weird side effects (e.g., running a legacy program as admin gives it a completely different set of settings). – Brian Sep 26 '13 at 12:51

3 Answers3

6

Consider using the isolated storage classes.

Isolated storage gives you a storage location that is associated with a given user and assembly; it works in not-fully-trusted environments and it can use roaming to keep the user's data present even on different machines. See

http://msdn.microsoft.com/en-us/library/System.IO.IsolatedStorage.aspx

for details.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
3

The special folders are, in fact, special because there is a reasonable guarantee that they will be present.

No other folders have such a guarantee. Even C:\ is not guaranteed (though it is quite likely to be present on the vast majority of Windows systems).

A custom folder under ApplicationData is a common place to write application specific data for a given user or CommonApplicationData for application data to be shared across all users on the system..

In environments where roaming is implemented (the user's data comes with him, no matter which physical server he logs onto), ApplicationData will follow the user. If you do not wish that behavior, you can use LocalApplicationData instead.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 2
    _reasonable guarantee_ ... read as: some IT departments may do weird things and lock these down to very minimal space or no access – jltrem Sep 25 '13 at 19:31
2

AppData path is probably what you want. This folder in windows XP+ can be referenced with

string folder = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

Commonly an application creates a sub folder under this to store its data and it doesn't require any special permissions to read/write into.

Matt
  • 224
  • 1
  • 5