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 / C++ / MFC
  4. #How to get the file path of my finished program in the code?#

#How to get the file path of my finished program in the code?#

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelptutorialquestion
3 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
    Fredrik
    wrote on last edited by
    #1

    Hi! When I have built my program and executes the exec file my program looks for a database file I have connected to my program. The problem here is that the path to the file is hard coded so it looks for it in a certain location on the HD. What I want to do is charnge where it looks for the database. But I don't know how to get a string of the executive file. Is there any function that can get the execution path of this file? I have found in the database file where it returns this path. I want to change it so that it gets the location where the .exe file is run. Thank you for reading, (or even more importantly responding! :)

    R 1 Reply Last reply
    0
    • F Fredrik

      Hi! When I have built my program and executes the exec file my program looks for a database file I have connected to my program. The problem here is that the path to the file is hard coded so it looks for it in a certain location on the HD. What I want to do is charnge where it looks for the database. But I don't know how to get a string of the executive file. Is there any function that can get the execution path of this file? I have found in the database file where it returns this path. I want to change it so that it gets the location where the .exe file is run. Thank you for reading, (or even more importantly responding! :)

      R Offline
      R Offline
      realJSOP
      wrote on last edited by
      #2

      Here's three functions I use for getting the program path. JustPath() - extracts the path from a string containing a filename. AddBackSlash() - adds a backslash to the end of a string if it's needed. GetProgramPath() gets the path and module filename of the running program (calls JustPath and AddBackSlash). //------------------------------------------------------------------------------ Returns just the path if present of the specified filename string. //------------------------------------------------------------------------------ CString JustPath(LPCSTR f) { char drive[_MAX_DRIVE], dir[_MAX_DIR], file[_MAX_FNAME], ext[_MAX_EXT]; CString st; _splitpath((char *)f, drive, dir, file, ext); st = drive; st += dir; return st; } //----------------------------------------------------------------------------/ // Add a back slash to the end of the path if it doesn't exist. //----------------------------------------------------------------------------/ void AddBackSlash(CString& path) { int length = path.GetLength(); if (length > 1) { if (path.GetAt(length - 1) != '\\') { path += "\\"; } } else if (1 == length) { CString temp = path; temp.MakeUpper(); TCHAR ch = temp.GetAt(0); if (ch >= 'A' && ch <= 'Z') { path += ":\\"; } else if (ch != '\\') // add a back slash anyway { path += "\\"; } } else { path += "\\"; } } //-----------------------------------------------------------------------------/ // Get the path were the program was run from //-----------------------------------------------------------------------------/ CString GetProgramPath(BOOL bStripFileName/*=TRUE*/) { CString sPath; TCHAR szFullPath[MAX_PATH]; ::GetModuleFileName(NULL, szFullPath, MAX_PATH); if (bStripFileName) { sPath = JustPath(szFullPath); AddBackSlash(sPath); } return sPath; }

      F 1 Reply Last reply
      0
      • R realJSOP

        Here's three functions I use for getting the program path. JustPath() - extracts the path from a string containing a filename. AddBackSlash() - adds a backslash to the end of a string if it's needed. GetProgramPath() gets the path and module filename of the running program (calls JustPath and AddBackSlash). //------------------------------------------------------------------------------ Returns just the path if present of the specified filename string. //------------------------------------------------------------------------------ CString JustPath(LPCSTR f) { char drive[_MAX_DRIVE], dir[_MAX_DIR], file[_MAX_FNAME], ext[_MAX_EXT]; CString st; _splitpath((char *)f, drive, dir, file, ext); st = drive; st += dir; return st; } //----------------------------------------------------------------------------/ // Add a back slash to the end of the path if it doesn't exist. //----------------------------------------------------------------------------/ void AddBackSlash(CString& path) { int length = path.GetLength(); if (length > 1) { if (path.GetAt(length - 1) != '\\') { path += "\\"; } } else if (1 == length) { CString temp = path; temp.MakeUpper(); TCHAR ch = temp.GetAt(0); if (ch >= 'A' && ch <= 'Z') { path += ":\\"; } else if (ch != '\\') // add a back slash anyway { path += "\\"; } } else { path += "\\"; } } //-----------------------------------------------------------------------------/ // Get the path were the program was run from //-----------------------------------------------------------------------------/ CString GetProgramPath(BOOL bStripFileName/*=TRUE*/) { CString sPath; TCHAR szFullPath[MAX_PATH]; ::GetModuleFileName(NULL, szFullPath, MAX_PATH); if (bStripFileName) { sPath = JustPath(szFullPath); AddBackSlash(sPath); } return sPath; }

        F Offline
        F Offline
        Fredrik
        wrote on last edited by
        #3

        NT

        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