.Net ramblings
# Saturday, 18 February 2006
HowTo: Get Write Access to an ApplicationData folder in 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. 

Saturday, 18 February 2006 19:11:26 (GMT Standard Time, UTC+00:00)  #    Comments [2]  Asp.Net

Monday, 03 April 2006 12:38:36 (GMT Daylight Time, UTC+01:00)
nice to hear that
Wednesday, 03 May 2006 19:44:31 (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
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview