0

I am trying to get my head over FileHelpers library and have main critical problem. When I try to implement it in my web app, or even use online demo I end up with "FileNotFoundException". The chosen file is being looked for on my C: drive. How can I make the FileHelpers code to access relative path to my application instead of absolute one?

(screenshot from online demo)

Regards,

Bartosz

Bartosz
  • 4,542
  • 11
  • 43
  • 69

1 Answers1

1

Use the Server.MapPath() method to map a relative path (based on current directory or web-site root) to an absolute accessible path.

For example, if yourfile.txt is placed inside App_Data folder of your web-site then you can write:

Customer[] customers =
    (Customer[])engine.ReadFile(Server.MapPath("~/App_Data/yourfile.txt"));

The tilde character represents the root of your web-site, if you specify a relative path then it'll be resolved as relative to the directory where your ASP.NET page resides.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • A new error appeared: The type or namespace name 'MapPath' does not exist in the namespace 'Microsoft.SqlServer.Server' (are you missing an assembly reference? – Bartosz Sep 07 '12 at 16:11
  • Worked with my web app though. Thanks – Bartosz Sep 07 '12 at 16:22