First, get the absolute path of the current running directory
1. Use GetCurrentDirectory function
Assuming the program path is D:\Test\tst.exe, 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, and so on, the improper setting causes the current path value change to be retrieved again. So if you want to avoid the current path change, if you use CFileDialog, 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);
Can also, the first execution getcurrentdirectory get to the directory path to save, processing completed, again setcurrentdirectory set.
2. Use GetModuleFileName
CString Strcurpath;
GetModuleFileName (Null,strcurpath.getbuffer (MAX_PATH), MAX_PATH);
int pos= strcurpath.reversefind (_t (' \ "));
Strcurpath = Strcurpath.left (POS);
Output (path includes run file name):
Strcurpath= "D:\Test\tst.exe"
Second, get the full path to open file
When you open a file through a dialog box, you generally need to get the full path to the open file, using the CFileDialog getpathname function, which 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 (hfiledlg.domodal () = = Idok) {
m_stredtsrcfile = Hfiledlg.getpathname ();
UpdateData (FALSE);
Third, get the current run program name
After obtaining the full path through GetModuleFileName, you can use the Pathstrippath API function to resolve the path, as follows:
/* #include "shlwapi.h"
#pragma comment (lib, "Shlwapi.lib")
* *
TCHAR szpath2[] = TEXT ("d:\\test\\ Tst.exe ");
Pathstrippath (szPath2);
Result:szpath2 ==tst.exe
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);
Use this function to note that the file name is not just extracted, and if it is not recognized, the original string is returned (no processing). Therefore, do not trust the words, their own manual extraction file name more insurance.
CString Strcurpath;
GetModuleFileName (Null,strcurpath.getbuffer (MAX_PATH), MAX_PATH)
strcurpath.releasebuffer ();//Must ReleaseBuffer, or getlength=0
int pos= strcurpath.reversefind (_t (' \ \));
int len = Strcurpath.getlength ();
Strcurpath = Strcurpath.right (len-pos-1);
Resources:
Http://msdn.microsoft.com/en-us/library/windows/desktop/bb773756%28v=vs.85%29.aspx
CString Weird GetLength () back to 0
If the CString is returned from GETPRIVATEPROFILESTR ()
Remember to return the CString before releasing the buffer
Otherwise, cstring.getlength () will return 0
Looking for half a day to reason,
----------------------------------------------------
' If You use the ' pointer returned by GetBuffer ' to ' change the string contents, ' must call ReleaseBuffer before using any Other CString member functions. ”
It is OK to call M_szfilename.releasebuffer () before I=m_szfilename.getlength ().
Temporarily engaged in two days VC, in VC How to get the name and path of the current program and how to split the string
#include "stdafx.h"
#include <windows.h>
int main (int argc, char* argv[])
{
//Get the full path of the running program first
Char szfilename[256];
memset (szFileName, ' 0 ', sizeof (szFileName));
GetModuleFileName (null,szfilename, sizeof (szFileName));
The string that divides the full path again, the last one is the program's name
char seps[] = "\";
char *token = NULL;
Char exename[256];
memset (EXEName, ' the ", sizeof (EXEName));
token = Strtok (szFileName, seps);
while (token!= NULL)
{
sprintf (exename, "%s", token);
token = Strtok (NULL, SEPs);
}
printf ("%s\n", exename);
GetChar ();
return 0;
}