Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What guaranteed file writing locations are available?

Tags:

c#

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

like image 645
Christopher Stevenson Avatar asked Sep 25 '13 19:09

Christopher Stevenson


3 Answers

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.

like image 104
Eric Lippert Avatar answered Oct 26 '22 00:10

Eric Lippert


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.

like image 3
Eric J. Avatar answered Oct 25 '22 23:10

Eric J.


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.

like image 2
Matt Avatar answered Oct 26 '22 01:10

Matt