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. Hardcoded filepaths c#

Hardcoded filepaths c#

Scheduled Pinned Locked Moved C#
csharpdatabasehelpasp-nettutorial
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.
  • F Offline
    F Offline
    frank21
    wrote on last edited by
    #1

    Hi all, I am experiencing a problem and I am not sure how to go about fixing it. I have an ASP.net app that is reading from a database. I have a default database file path hardcoded into my program that the program will use if the user does not specify a database file. When executed, if the program tries to access the default database it is failing. It seems like .net is adding a "C:" to my file path where it should be "\\dir1\\dir2\\DB.mdb" Can anyone help me out so that I can access the default database on the LAN. Thanks again :-D

    H 1 Reply Last reply
    0
    • F frank21

      Hi all, I am experiencing a problem and I am not sure how to go about fixing it. I have an ASP.net app that is reading from a database. I have a default database file path hardcoded into my program that the program will use if the user does not specify a database file. When executed, if the program tries to access the default database it is failing. It seems like .net is adding a "C:" to my file path where it should be "\\dir1\\dir2\\DB.mdb" Can anyone help me out so that I can access the default database on the LAN. Thanks again :-D

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      NEVER hard code a file path. Put the filepath in the appSettings section at the bottom of the Web.config file and use ConfigurationSettings.AppSettings to get it (defaulting to a hard-coded path if you like). When you do it this way, do not escape back-slashes (that's specific to a programming language - not strings). If you want to use a file from a share, then use "\\\\SERVER\\SHARE\\Dir1\\DB.mdb" or @"\\SERVER\SHARE\Dir1\DB.mdb" (notice the @, which is a literal character identifier). Many of the IO members - unless you give them a URL - will assume a local path, which is why "C:" is being prepended to your string (well, the string isn't actually changed but the behavior is the same). If this file is in your virtual directory structure of your web application, 1) make sure it's protected from user's downloading it but so that the web application can access it, and 2) use Server.MapPath or Page.MapPath to map the virtual path (like /db/db.mdb) to a physical path (like C:\Inetpub\wwwroot\db\db.mdb).

      Microsoft MVP, Visual C# My Articles

      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