tags:

views:

2140

answers:

2

In ASP.Net there is the app_data folder which is not accessible to users via the web. I would like to store a file in this folder, and read from this file by using a class that I have in the App_Code folder.

Keep in mind that the current working directory here is going to be a directory such as "c:\","c:\windows\system32\", etc. and not the application directory. So something like "../App_Data/somefile" does not work.

There has to be something simple that I overlooked on how to access files in the App_Data folder programatically

How can I do this?

A: 

You say you don't have access to context object but it's unclear why that would be.

If your code runs in response to a request then you can access the context as :-

HttpContext.Current

Failing that you can get to the root physical path for the application using:-

HttpRuntime.AppDomainAppPath
AnthonyWJones
+1  A: 

You can access the App_Data folder like so:

string appdatafolder = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "App_Data");

I realise that this uses the HttpContext object, however, if you're writing this code inside a custom ASP.NET Membership provider (something I've done myself a number of times), you should have no problem using the HttpContext object.

CraigTP
Thanks, this got me off to the right start, I went for HttpContext.Current.Server.MapPath("~/App_Data/myfilenamehere") approach as it seemed more readable.
Brian
@Brian - Glad you're up and running. See this link, though, regarding performance of MapPath vs PhysicalApplicationPath: http://dotnetperls.com/Content/PhysicalApplicationPath.aspx May not be a problem if only accessing occasionally, but nice to be aware of the performance differences.
CraigTP