Search code examples
asp.netexceptionfilehelpers

FileHelpers file access


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


Solution

  • 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.