Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to reach a file under a folder inside a solution in VS C#

How to reach a file under a folder inside a solution in VS C#

Scheduled Pinned Locked Moved C#
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jasonalien
    wrote on last edited by
    #1

    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.

    OriginalGriffO 1 Reply Last reply
    0
    • J jasonalien

      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.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

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

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups