To determine the type of a logical drive, you must call the getdrivetype function. It uses the path name as a parameter (such as c: \) and returns drive_fixed, drive_removable, or drive_unknown. All possible returned values are listed below: these values are defined in WINBASE. h.
# Define drive_unknown 0 // invalid path name
# Define drive_no_root_dir 1 // The path is invalid. For example, the volume label cannot be found.
# Define drive_removable 2 // removable drive (such as disk drive and optical drive)
# Define drive_fixed 3 // fixed drive (such as a general hard disk)
# Define drive_remote 4 // network drive
# Define drive_cdrom 5 // CD-ROM
# Define drive_ramdisk 6 // Random Access (RAM) disk
Public const file _ "attribute_archive = & H20
Public constFile_attribute_ Compressed = & h800
Public constFile_attribute_ Directory = & "H10
Public constFile_attribute_ Hidden = & H2
Public constFile_attribute_ "Normal = & h80
Public constFile_attribute_ Readonly = & H1
Public constFile_attribute_ "System = & h4
Public constFile_attribute_ "Temporary = & h100
Functions related to the drive include getlogicaldrives, getlogicaldrivestrings, and getdrivetype. The first two are used to obtain the logical drive.Drive letter, Getlogicaldrivestrings returns the path name string, for example:
":\ <Null> C :\< null> F: \ <null> "
Each path name is separated by null (null or zero) characters and ends with two null characters. This is a standard C-style processing method. Getlogicaldrives is a useful API function for compilation language users who like bit and byte operations. It returns the logical drive in the form of a bit mask. That is, in the return value of a DWORD type, bit 0 (the smallest one) indicates drive a, bit 1 indicates drive B, and so on. If the status of each bit is on, the corresponding logical drive exists; otherwise, the status is off, indicating that the corresponding logical drive does not exist. We all know that DWORD is a 32-bit value, which can contain up to 26 letters.Drive letter.
Tchar Buf [100];
DWORD Len = getlogicaldrivestrings (sizeof (BUF)/sizeof (tchar), Buf );
// Delete the stuff in the Dir directory in MFC
Void pai_rd (cstring DIR)
{
Win32_find_data SR;
Handle handle;
Int iattr;
// If it is a directory
Iattr = getfileattributes (DIR );
If (iattr = file_attribute_directory)
{
Try
{
Handle =: findfirstfile (DIR + "\ *. *", & SR );
}
Catch (...)
{
Return;
}
If (handle)
{Do
{
If (Sr. cfilename [0]! = '.')
{
If (Sr. dwfileattributes = file_attribute_directory)
{
Performance_rd (DIR + "file: // % 22 + Sr. cfilename /);
}
Else
{
: Setfileattributes (DIR + "file: // % 22 + Sr. cfilename, 0 /);
: Deletefile (DIR + "file: // % 22 + Sr. cfilename /);
}
}
} While (: findnextfile (handle, & SR ));
: Findclose (handle );
}
If (iattr = file_attribute_directory)
: Removedirectory (DIR );
}
Else
{
: Setfileattributes (Dir, 0 );
: Deletefile (DIR );
} // Clear the console screen under MFC
Void cmd_cls (handle hconsole)
{
Coord coordscreen = {0, 0}; // home for the cursor DWORD ccharswritten; console_screen_buffer_info CSBi; DWORD dwconsize; // get the number of character cells in the Current Buffer. If (! Getconsolescreenbufferinfo (hconsole, & CSBi) return; dwconsize = CSBi. dwsize. x * CSBi. dwsize. Y; // fill the entire screen with blanks. If (! Fillconsoleoutputcharacter (hconsole, (tchar) '', dwconsize, coordscreen, & ccharswritten) return; // get the current text attribute. If (! Getconsolescreenbufferinfo (hconsole, & CSBi) return; // set the buffer's attributes accordingly. If (! Fillconsoleoutputattribute (hconsole, CSBi. wattributes, dwconsize, coordscreen, & ccharswritten) return; // put the cursor at its home coordinates. setconsolecursorposition (hconsole, coordscreen);} // copy a folder in MFC
Void pai_xcopy (char * SRC, char * DST)
{
Win32_find_data findfiledata;
Handle hfind;
Char tmpsrc [2, 256];
Strcpy (tmpsrc, Src );
Strcat (tmpsrc ,"\\*.*");
Hfind = findfirstfile (tmpsrc, & findfiledata );
If (hfind = invalid_handle_value)
Return;
Createdirectory (DST, 0 );
Do
{
Char newdst [256];
Strcpy (newdst, DST );
If (newdst [strlen (newdst)]! = '\\')
Strcat (newdst ,"\\");
Strcat (newdst, findfiledata. cfilename); char newsrc [1, 256];
Strcpy (newsrc, Src );
If (newsrc [strlen (newsrc)]! = '\\')
Strcat (newsrc ,"\\");
Strcat (newsrc, findfiledata. cfilename );
If (findfiledata. dwfileattributes & file_attribute_directory)
{
If (strcmp (findfiledata. cfilename ,".")! = 0 & strcmp (findfiledata. cfilename ,"..")! = 0)
{
Performance_xcopy (newsrc, newdst );
}
} Else
{
Copyfile (newsrc, newdst, false );
}
} While (findnextfile (hfind, & findfiledata ));
Findclose (hfind);} // create the bool wincmd: pai_md (char * lppath) directory in MFC)
{Cstring pathname = lppath; If (pathname. Right (1 )! = "\") Pathname + = "\"; int end = pathname. reversefind ('\'); int Pt = pathname. find ("\"); If (pathname [pt-1] = ':') Pt = pathname. find ("\", Pt + 1); cstring path;
While (PT! =-1 & PT <= end)
{
Path = pathname. Left (Pt + 1 );
If (_ access (path, 0) =-1)
_ Mkdir (PATH );
PT = pathname. Find ("\", Pt + 1);} return true ;}
DWORD dwattrs;
Dwattrs = getfileattributes ("C: \ Boot. ini ");
If (dwattrs & file_attribute_readonly)
{
// Read-only
}
Other attributes include:
File_attribute_hidden
File_attribute_directory
You can use "&" bit operations to differentiate them;
For example, if you want to know whether it is a directory, you can:
Dwattr = getfileattributes (lpctstr lpfilename );
If (dwattr & file_attribute_directory) = 0) // not a directory
;
Else // is the Directory
;