0

I'm using this code as part of class which should connect to an Access database:

string path = HttpContext.Current.Server.MapPath("App_Data");

The App_Data folder is in the project, and contains the database, but somehow the MapPath() returns null.

How can I solve this?

Grant Winney
  • 63,310
  • 12
  • 110
  • 157

1 Answers1

0

Try using:

string folder = System.Web.HttpContext.Current != null ?
            System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data") :
            System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
JsonStatham
  • 8,783
  • 26
  • 95
  • 168