SoFunction
Updated on 2025-03-10

VC gets the implementation code of the current path and program name

1. Get the absolute path to the current running directory

1. Use the GetCurrentDirectory function
Assuming the program path is D:\Test\, execute the GetCurrentDirectory function

char pBuf[MAX_PATH];
GetCurrentDirectory(MAX_PATH,pBuf);

pBuf="D:\Test"
However, if you use functions such as CFileDialog, CFile::Open, etc., the settings are inappropriate, which will cause the current path value to be retrieved again. Therefore, if you want to avoid the current path change, if you use CFileDialog, you should set the dwFlags flag in CFileDialog to OFN_NOCHANGEDIR. as follows:

CFileDialog hFileDlg(false,NULL ,
		NULL,
		OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR,
		TEXT("Text Files (*.txt)|*.txt|All Files(*.*)|*.*|"),
		NULL);

Also, first execute GetCurrentDirectory to save the get directory path. After the processing is completed, set SetCurrentDirectory again.
2. Use GetModuleFileName

CString strCurPath;
	GetModuleFileName(NULL,(MAX_PATH),MAX_PATH);
 	int pos= (_T('\\'));
 	strCurPath = (pos);

Output (path includes run file name):
strCurPath="D:\Test\"
2. Get the complete path to the open file

When opening a file through a dialog box, you generally need to get the complete path to the open file. You can use the GetPathName function of CFileDialog, the code is as follows:

CFileDialog hFileDlg(false,NULL ,
	NULL,
	OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR,
	TEXT("Text Files (*.txt)|*.txt|All Files(*.*)|*.*|"),
	NULL);
if(() == IDOK) {
	m_strEdtSrcFile = ();
	UpdateData(FALSE);
}

3. Get the current running program name

After obtaining the full path through GetModuleFileName, you can use the PathStripPath API function to parse the path. The code example is as follows:

/*
#include ""
#pragma comment(lib, " ")
*/

TCHAR szPath2[] = TEXT("D:\\Test\\");
PathStripPath(szPath2);
// Result: szPath2 ==
 
TCHAR szPath3[] = TEXT("D:\\Test\\Debug");
PathStripPath(szPath3);
// Result: szPath3 == Debug

TCHAR szPath4[] = TEXT("D:\\Test\\Debug\\");
PathStripPath(szPath4);
// Result: szPath4 == Debug\

TCHAR szPath5[] = TEXT("D:\\");
PathStripPath(szPath5);
// Result: szPath5 == D:\ 

Note that using this function is not just extracting the file name, if it is not recognized, the original string will be returned (no processing). Therefore, if you are worried, it is safer to manually extract the file name yourself.

CString strCurPath;
GetModuleFileName(NULL,(MAX_PATH),MAX_PATH)
();//Must ReleaseBuffer, or GetLength=0
int pos= (_T('\\'));
int len = ();
strCurPath = (len-pos-1);

References:

/en-us/library/windows/desktop/bb773756%28v=vs.85%

CString weird GetLength() returns 0

If CString is returned from GetPrivateProfileStr()
Remember to return the CString before releasing the buffer
Otherwise, () will return 0
I've been looking for the reason for a long time.
----------------------------------------------------
“If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. ”

Just call m_SZFileName.ReleaseBuffer() before i=m_SZFileName.GetLength().

Temporarily engage in VC for two days, how to get the name and path of the current program in VC and how to split the string

#include ""
#include <> 
 

int main(int argc, char* argv[])

{
 //First get the complete path to run the program char szFileName[256];
 memset(szFileName,'"0',sizeof(szFileName));

 GetModuleFileName(NULL,szFileName, sizeof(szFileName)); 

 //Split the string of the complete path, the last one is the name of the program char seps[] = "\\" ; 
 char *token = NULL; 
 
 char exeName[256];
 memset(exeName,'\0',sizeof(exeName));
 
 token = strtok( szFileName, seps ); 
 while( token != NULL ) 
 { 
   sprintf(exeName,"%s",token);
   token = strtok( NULL, seps ); 
   
  }
  printf("%s\n",exeName); 
  getchar();
  return 0;
}