1. To determine whether a file exists
Using CFile class and CFileStatus class to judge
CFileStatus Filestatus;
if (CFile::GetStatus (_t ("D://softist.txt"), Filestatus)
AfxMessageBox (_t ("file exists"));
Else
AfxMessageBox (_t ("file does not exist");
Using the CFileFind class to judge
CFileFind FileFind;
CString strpathname = _t ("D://softist.txt");
if (filefind. FindFile (strpathname))
AfxMessageBox (_t ("file exists"));
Else
AfxMessageBox (_t ("file does not exist");
Using API function FindFirstFile, this function can also determine attributes such as file attributes, date, size, and so on. Cases:
Win32_find_data Findfiledata;
HANDLE Hfind;
Hfind = FindFirstFile (_t ("D://softist.txt"), &findfiledata);
if (hfind = = INVALID_HANDLE_VALUE)
{
AfxMessageBox (_t ("file does not exist");
}
Else
{
AfxMessageBox (_t ("file exists"));
FindClose (hfind);
}
2. File date operation. The following is to obtain "d://softist.txt" File modification time, trace, and then the file modification time to 2000-12-03 12:34:56.
HANDLE hfile;
filetime filetime;
filetime localtime;
SYSTEMTIME SYSTEMTIME;
hfile = CreateFile (_t ("D://softist.txt"), Generic_read | Generic_write,
0, NULL, open_existing, file_attribute_normal, null);
if (hfile!= invalid_handle_value)
{
getfiletime (hfile, NULL, NULL, &filetime); Get UTC File Time
filetimetolocalfiletime (&filetime, &localtime);//change to local time
filetimetosystemtime (&localtime, &systemtime); Change to System time format
TRACE ("%04d-%02d-%02d%02d:%02d:%02d/r/n",
systemtime.wyear, Systemtime.wmonth, Systemtime.wday,
Systemtime.whour, Systemtime.wminute, systemtime.wsecond);
//Change the file time into 2000-12-03 12:34:56
systemtime.wyear = Systemtime.wmonth = Systemtime.wday = 3;
Systemtime.whour = Systemtime.wminute = Systemtime.wsecond =;
SystemTimeToFileTime (&systemtime, &localtime);//change file time format
localfiletimetofiletime (&localtime, &filetime);//change to UTC time
setfiletime (hfile, NULL, NULL, &filetime); Set UTC file Time
CloseHandle (hfile);
}
3. Set file properties
BOOL setfileattributes (LPCTSTR lpfilename, DWORD dwfileattributes);
The meaning of dwFileAttributes
File_attribute_archive Save File
File_attribute_hidden hidden files
File_attribute_normal usual files
File_attribute_readonly read-only file
File_attribute_system System files
Cases:
SetFileAttributes (_t ("D://softist.txt", file_attribute_readonly);
copying, moving, deleting, renaming of files
1. Replication API for files
BOOL CopyFile (LPCTSTR lpexistingfilename, LPCTSTR lpnewfilename, bool bfailifexists);
Bfailifexists is used to determine whether to abort the copy operation if the target file already exists, and return false. For example, "D://softist1.txt" is copied to "D://softist2.txt", even if "d://softist2.txt" already exists.
BOOL BRet = CopyFile (_t ("D://softist1.txt"), _t ("D://softist2.txt"), FALSE);
2. Mobile API for files
BOOL MoveFile (LPCTSTR lpexistingfilename, LPCTSTR lpnewfilename);
This function can be moved to a file, or directory (including subdirectories), for example,
MoveFile (_t ("D://softist.txt"), _t ("D://softist2.txt"));
The following APIs carry the option dwflags, moving files, or directories (including subdirectories).
BOOL MoveFileEx (LPCTSTR lpexistingfilename, LPCTSTR lpnewfilename, DWORD dwflags);
The meaning of dwflags:
movefile_replace_existing if the target file exists to replace it.
Movefile_delay_until_reboot file Move preparation and perform the move job the next time the system is started.
3. deleting files
Api:
BOOL DeleteFile (LPCTSTR lpfilename);
Mfc:
static void PASCAL Cfile::remove (LPCTSTR lpszfilename);
4. File renamed MFC:
tchar* poldname = _t ("Oldname_file.dat");
tchar* pnewname = _t ("Renamed_file.dat");
Try
{
cfile::rename (Poldname, pnewname);
}
catch (cfileexception* pEx)
{
TRACE (_t ("File%20s not found, cause =%d/n"), Poldname,
pex->m_cause);
Pex->delete ();
}
Traverse file Directory
Traverse the file directory, that is, the file in a directory and subdirectories of the file names out. This article is a note of an example of a CFileFind class. The following program starts from a directory and puts all the members in this directory on the level trace to the debug output screen.
void Travelfolder (CString strdir, int ndepth)
{
CFileFind FileFind; Declaring CFileFind type variables
CString Strwildpath = Strdir + _t ("//*.*"); All files are listed.
if (FileFind. FindFile (Strwildpath, 0))//Start retrieving files
{
BOOL bRet = TRUE;
while (BRet)
{
BRet = FileFind. FindNextFile (); Enumerating a file
if (FileFind. Isdots ())//if yes. Or.. Do the next
Continue
for (int i = 0; i < ndepth i + +)//Level space print
{
TRACE (_t (""));
}
if (!filefind. Isdirectory ())//is not a subdirectory, print out the file name
{
CString strtextout = Strdir + CString (_t ("//")) + FileFind. GetFileName ();
TRACE (_t ("file =%s/r/n"), strtextout);
}
else//If it is a subdirectory, call the function recursively
{
CString strtextout = Strdir + CString (_t ("//")) + FileFind. GetFileName ();
TRACE (_t ("dir =%s/r/n"), strtextout);
Travelfolder (strtextout, ndepth + 1);//recursively call the function to print a file in a subdirectory
}
}
FileFind. Close ();
}
}
Test, print all files and subdirectories in the/temp of D disk to the debug output screen.
void Test ()
{
Travelfolder (CString (_t ("D://temp")), 0);
}
File directory Operations
1. Create a directory (API)
BOOL createdirectory (LPCTSTR pstrdirname);//pstrdirname is full path
2. Delete directory (API)
BOOL removedirectory (LPCTSTR lppathname);
3. To determine whether a directory exists (Shell Function)
#include <shlwapi.h> #pragma comment (lib, "Shlwapi.lib") if (Pathisdirectory (_t ("D://temp")) AfxMessageBox (_t ( "Existence"); else AfxMessageBox (_t ("nonexistent"));
4. Get current directory (API)
DWORD GetCurrentDirectory (DWORD nbufferlength, LPTSTR lpbuffer);
5. Gets the directory where the execution file is located (API)
DWORD GetModuleFileName (Hmodule hmodule, LPTSTR lpFileName, DWORD nsize);
6. Get feature directory (Shell function)
BOOL SHGetSpecialFolderPath (HWND hWndOwner, LPTSTR lpszpath, int nfolder, BOOL fcreate);
Example: Read my file directory
TCHAR szdirfile[1024];
memset (szdirfile, 0, sizeof (szdirfile));
BOOL BRet = SHGetSpecialFolderPath (null,szdirfile,csidl_personal,true);
if (BRet)
{
AfxMessageBox (Szdirfile);
}
7. Select the dialog box interface for the directory
Use Shell function to make a dialog box interface for selecting a directory
#include <shlobj.h>
INT CALLBACK _browsecallbackproc (HWND hwnd, UINT umsg, LPARAM LPARAM, LPARAM pData)
{
TCHAR Szdir[max_path];
Switch (umsg)
{
Case bffm_initialized:
WParam is TRUE since you are passing a path.
It would is FALSE if you were passing a pidl.
SendMessage (hwnd, Bffm_setselection, TRUE, (LPARAM) pData);
Break
Case bffm_selchanged:
Set the Status window to the currently selected path.
if (SHGetPathFromIDList (lpitemidlist) LParam, Szdir))
{
SendMessage (hwnd,bffm_setstatustext,0, (LPARAM) szdir);
}
Break
}
return 0;
}
CString Getfolderfullpath (LPCTSTR lpszdefault)
{
TCHAR Buffdisplayname[max_path];
TCHAR Fullpath[max_path];
Browseinfo Browseinfo;
Lpitemidlist lpitemidlist;
ZeroMemory (&browseinfo, sizeof (Browseinfo));
Browseinfo.pszdisplayname = Buffdisplayname;
Browseinfo.lpsztitle = _t ("Please select Directory");
Browseinfo.ulflags = Bif_returnonlyfsdirs;
Browseinfo.lparam = (lParam) Lpszdefault;
BROWSEINFO.LPFN = _browsecallbackproc;
if (!) ( Lpitemidlist = SHBrowseForFolder (&browseinfo)))
{
AfxMessageBox (_t ("No Directory Selected"));
Return CString (_t (""));
}
Else
{
SHGetPathFromIDList (Lpitemidlist, FullPath);
CoTaskMemFree (lpitemidlist);
Return CString (FullPath);
}
}
void Ctest77dlg::onbnclickedbutton1 ()
{
CString Strfolderfullpath = Getfolderfullpath (_t ("d://temp"));
if (Strfolderfullpath!= _t (""))
AfxMessageBox (Strfolderfullpath);
}