Operations on files

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.