#How to get the file path of my finished program in the code?#
-
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! :)
-
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! :)
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; }
-
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; }