ASP.Net applications have a special folder call App_Data, which is specifically designed to hold application-data such as databases. SQL-Server express will place it’s .mdf files here by default.
Because this directory is being protected by the .Net-framework and thus not accessible by clients, it’s a good idea to place other data here as well.
To access this folder a couple of different approaches come to mind. The first idea would be to just access
/App_Data/mydata.db hardcoded. While this might work in most scenarios, it will fail if the application is installed in a virtual directory instead of the root.
OK, let’s try something different. How about
~/App_Data/mydata.db? Looks compelling. At runtime the .Net-framework will substitute “~” with the actual base-directory of the application. Unfortunately this is only done for certain elements. So this might not always work. You could of course get the actual path on the server side by using
Doing some research offers my preferred approach
AppDomain.CurrentDomain.GetData("DataDirectory") will return the path to the App_Data folder wherever that’s actually may be. Nice.