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
  • 65,241
  • 13
  • 115
  • 165

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
  • 9,770
  • 27
  • 100
  • 181