How to reach a file under a folder inside a solution in VS C#
-
In my project, there is a folder called "Resources" and there is an Excel file called "query.xlsx" inside that folder. I try to load Excel file like this :
ExcelFile ef = ExcelFile.Load(HttpContext.Current.Server.MapPath("~/Resources/query.xlsx"));
And I get this exception : HttpContext.Current.Server = 'HttpContext.Current.Server' threw an exception of type 'System.NullReferenceException' How can I load that Excel file properly? Thanks.
-
In my project, there is a folder called "Resources" and there is an Excel file called "query.xlsx" inside that folder. I try to load Excel file like this :
ExcelFile ef = ExcelFile.Load(HttpContext.Current.Server.MapPath("~/Resources/query.xlsx"));
And I get this exception : HttpContext.Current.Server = 'HttpContext.Current.Server' threw an exception of type 'System.NullReferenceException' How can I load that Excel file properly? Thanks.
MapPath is only used with websites: it "communicates" with IIS and converts "~" into the root folder fro your site. For non-website based applications the HttpContext and the Server don't exist, so you get a null reference. If the file changes at all, then consider storing it elsewhere: Where should I store my data?[^] will help. If it doesn't change at all, then embed it as a resource to your application, and use Properties.Resources to fetch the file data.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...