# Saturday, February 18, 2006
Saturday, February 18, 2006 7:11:26 PM (GMT Standard Time, UTC+00:00) ( Asp.Net )
In a web application i'm working on, i needed to get write access to a folder, and preferably not the temp folder because the data should be kept reasonably safe from being deleted accidentally.  If it was a windows-forms application this would be easy, i would just use Application.UserAppDataPath.  So i thought for asp.net, i could use Environment.SpecialFolder.ApplicationData but this maps to the folder for the current user logged on to Windows, not the process running the web site, or the authenticated web site user.  So i just skip up 2 parent directories from that, and use the current Principal.WindowsIdentity to find the actual process running ASP.NET, and then deduce the correct folder, which by default will have write permissions for Asp.Net.

Here is the code:
string Username = Path.GetFileName(System.Security.Principal.WindowsIdentity.GetCurrent().Name).Replace(" ", "");
string UserAppFolder = String.Format(@"{0}\{1}\Application Data", Directory.GetParent(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName).FullName, Username);

By the way, the use of Path.GetFileName is entirely intentional, it solves the case where the current user is "NT Authority\Network Service" by yielding "Network Service", which is what we are interested in.  

This will hopefully come in handy for my 'zero configuration' idea for the application.  Next time you need a write folder, it may be a good idea to use this instead of making users specify write permissions for a custom folder. 
Comments [2] | | # 
Monday, April 03, 2006 12:38:36 PM (GMT Daylight Time, UTC+01:00)
nice to hear that
Wednesday, May 03, 2006 7:44:31 PM (GMT Daylight Time, UTC+01:00)
Hi

I've been working with vb.net smartdevice and need to get the program files folder path.

i've got this code:

Dim programFiles As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)

but get this message: GetFolderPath y SpecialFolder is no member of System.Environment.

do you have any solutions?


Bernardo
Bernardo
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview