25

I need to read a file there. I tried Assembly.ExecutingAssembly etc but that doesnt work.

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 1
    possible duplicate of [ASP.NET MVC - Find Absolute Path to the App_Data folder from Controller](http://stackoverflow.com/questions/1268738/asp-net-mvc-find-absolute-path-to-the-app-data-folder-from-controller) – Kev Apr 08 '11 at 09:06

4 Answers4

27

From asp.net it's simple:

Server.MapPath("~/App_Data");//anywhere
realMarkusSchmidt
  • 4,303
  • 1
  • 29
  • 33
Alberto León
  • 2,879
  • 2
  • 25
  • 24
  • 2
    Except sometimes it's not. Deployed websites often have their App_Data in `~/bin/App_Data`, but use `~/App_Data` locally. Using the `|DataDirectory|` substitution is broken in these cases. See http://stackoverflow.com/questions/18706945/datadirectory-returns-wrong-path-to-appdata-folder – Charles Burns Sep 16 '13 at 04:08
  • @CharlesBurns fixed, see at your question – Roland Dec 21 '15 at 13:39
  • I'm doing so in some cs class and the Server object was giving problems. HttpContext.Current.Server made it work. – Vasily Hall Apr 26 '17 at 20:25
  • @ozn is not common these days to use App_Data, except azure is using it to put Webjobs. Another question is the App_Data could be in bin directory as CharlesBurns metioned – Alberto León May 28 '19 at 09:24
12

another way is:

AppDomain.CurrentDomain.GetData("DataDirectory");
mina morsali
  • 778
  • 1
  • 16
  • 29
4

String base = AppDomain.CurrentDomain.BaseDirectory; //Using above code , you will get base directory of your applications. String pathToData_App = base + "/App_Data/SqLite.db";

Above code works for me.

Rakesh Chaudhari
  • 3,310
  • 1
  • 27
  • 25
3

I needed to execute ELMAH scripts for AppData so is used this

var elmahScript = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ELMAH-1.2-db-SQLServer.sql");
Ali Umair
  • 1,386
  • 1
  • 21
  • 42