1. Obtain the path of the execution File
Use the GetModuleFileName Function
2. Obtain the directory of the execution File
CString sFile, sPath;
// Obtain the path of the main program, which exists in sPath.
GetModuleFileName (NULL, sPath. GetBufferSetLength (MAX_PATH + 1), MAX_PATH );
SPath. ReleaseBuffer ();
Int nPos;
NPos = sPath. ReverseFind (''\'');
SPath = sPath. Left (nPos );
SFile = sPath + \ Demo.doc; // name of the Excel file to be read
3. Several methods to determine whether a file exists
BOOL CPubFunc: FileExist (CString FileName)
{
CFileFind fFind;
Return fFind. FindFile (FileName );
}
BOOL CPubFunc: DirectoryExist (CString Path)
{
WIN32_FIND_DATA fd;
BOOL ret = FALSE;
HANDLE hFind = FindFirstFile (Path, & fd );
If (hFind! = INVALID_HANDLE_VALUE) & (fd. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
{
// The directory exists.
Ret = TRUE;
}
FindClose (hFind );
Return ret;
}
BOOL CPubFunc: CreateDirectory (CString path)
{
SECURITY_ATTRIBUTES attrib;
Attrib. bInheritHandle = FALSE;
Attrib. lpSecurityDescriptor = NULL;
Attrib. nLength = sizeof (SECURITY_ATTRIBUTES );
Return: CreateDirectory (path, & attrib );
}