-------------------------------------------------- -------------
Design ideas
Under the Windows operating system, there is no way to modify the folder time attributes, even in the Win32 API function, but only provides function calls for file time properties, without folder time attributes. Modified words. Although the backup program provided by Windows can copy the time attributes of all subfolders under the folder to be backed up, but cannot keep the root directory time attribute constant. Thereby, it is possible to take the way to take a backup, and start from the Win32 API function associated with the backup. Specifically, you can open the folder in a way to open the file, and then you can get the original time attribute with the Win32 API function () originally used to process file time properties, and set it as a parameter. The folder time attributes after the backup, such processing ensures that the folder is consistent with the time of the backup.
According to the previous analysis, it can be seen that the folder in accordance with the open file is the key to the entire processing process. It is often used to create, open the file's Win32 API function creteFile () is not only used to create and open the file object. It can actually be used to create, open pipelines, mail slots, communication resources, disk drives (only for Windows NT), console, and folders (only open). The prototype of CreateFile () is given below:
Handle Createfile (LPCTSTR LPFILENAME, / / File Name Pointer
DWORD DWDESIREDACCESS, // Access Mode
DWORD DWSHAREMODE, // Shared Mode
LPSecurity_Attributes LPSecurityAttributes, // Security Properties
DWORD dwcreationdisposition, // creation method
DWORD dwflagsandattributes, // file properties
Handle HTemplateFile // Pointer to the copy of the properties
);
When the folder is opened with its folder, the first parameter LPFileName should be set to the name of the folder to be opened; as for the access mode, it can be flexible as needed. For this article, the source folder is only read. Therefore, it can be set to generic_read. For the folder behind the backup, since the attribute information is written, it needs to be supported by generic_write; the setting of the sharing mode parameter DWSHAREMODE is not different, where there is no difference in the settings when the file is processed, can be set to file_share_read | File_Share_Delete; Since the createFile () function does not perform folder operation, the creation method can only be opened, and the creation method can only open the existing object, that is, dwcreationDisposition should be set to Open_EXISTING; compared to the setting of the dwflagsandattributes parameter is compared Important, it is the operation of opening a folder by setting this parameter to file_flag_backup_semantics attribute.
Usually, the file and folder time property refers to a few specific properties such as creating time, recent access time, and recent modification time. For the above properties of the file, you can get GetFileTime (), for the folder, after opening it through the createfile () function, the handle of its acquired can be used as a file handle. Therefore, the time attribute of the folder can also be obtained by getFileTime () function. The getFiletime () function prototype is as follows: BOOL getFileTime (handle hfile, // file handle
LPFileTime LPCREATIONTime, // Create time address
LPFileTime Lplastaccesstime, // Recently Access Time Address
LPFileTime LplastWRittime // Recently modified the address of the time
);
The latter three parameters are pointers to the Filetime structure, and the UTC time is obtained, and if needed, the UTC time can be converted to the time. It is also possible to further translate it from the file time format into system time format via the filetimetosystemTIME () function, and the transformed time format will be saved in a SystemTime structure object. Similarly, when writing time information to the folder property, if it is not the file time format, it should be converted from the systemTimTIMETOFILETIME () function to the file time format, and then the setFileTime () function will specify the specified time. Write to the folder's time properties. This way, all folders including the root directory can hold the consistent time attribute during data backup and recovery. Simple example
Next, a simple application example is given in accordance with the previous description, and through this example, the time attribute information can be read from the specified folder and can be rewritten after modification (here only the recent modification time is processed here. Other time properties can be implemented in a similar approach). Here, through two functions getDirtime () and setdirtime () to implement acquisition and change processing of folder time information, the implementation process of these two functions is explained in the form of comments:
/ / Get the time attribute of the specified folder, the portal parameter dirname specifies the folder to be processed, and Stime is one
/ / Point to the pointer to the SystemTime structure
Bool CsetFordertimedlg :: getdirtime (cstring dirname) {
// Open the folder
Handle hdir = createfile (Dirname, Generic_Read,
File_share_read |file_share_delete,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL;
Filetime LPCREATIONTIME; // Creation Time of Folder
Filetime Lplastaccesstime; // Recent accessed time for folders
Filetime LplastWRitetime; // Recent modified time
// Get folder time attribute information
IF (GetFileTime (HDIR, & LPCREATIONTIME, & LPLASTACCESSTIME, & LPLASTWRITIME)) {
Filetime ftime;
FiletimetolocalFiletime (& lplastwritetime, & ftime); // Convert cost time
FiletimetosystemTime (& ftime, & stime); // Convert to System Time Format}
CloseHandle (HDIR); // Close the open folder
Return RetVal;
}
/ / Set the time property of the specified folder, the entrance parameter DIRNAME specifies the folder to be processed, new_time
/ / For a pointer to the SystemTIME structure
Bool csetfordertimedlg :: setdirtime (cstring dirname, systemtime new_stime) {
// Open the Win32 API call for the directory
Handle hdir = cretefile (dirname, generic_read |Generic_write,
File_share_read |file_share_delete,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL;
Filetime LPCREATIONTIME; // Creation Time of Folder
Filetime Lplastaccesstime; // Recent accessed time for folders
Filetime LplastWRittime; // Recent modified time on the folder
SystemTimetOfiletime (& new_stime, & lpcreationTIME); // Convert to file time format
SystemTimetOfiletime (& new_stime, & lplastaccesstime);
SystemTimetOfiletime (& new_stime, & lplastwritetime);
/ / Set the time attribute of the folder
Bool Retval = SetFileTime (HDIR, & lpCreationTime, & Lplastaccesstime, & lplastwrittime);
CloseHandle (HDIR); // Close folder
Return RetVal;
}
At this point, it is easy to implement acquisition and settings for any specified folder time attribute by calling the getDirtime () and setdirtime () functions, specifically:
SystemTime Stime; // System Time Structure Object
IF (getdirtime (m_path, stime))
{
// If the folder time property is obtained, the obtained time information will be saved in the STIME structure object.
......
// If you need to modify the acquired time attribute, you can still keep the constant
......
// Write the modified time attribute back to folder
SetDirtime (m_path, stime);
}
summary
This article opens the folder through the createFile () function, and treats it in the later processing, it can be used to obtain a function of getFileTime (), setFileTime () to obtain the time attribute to obtain and write it, including Any folder in the root directory for time property settings. There is a good application prospect in the full backup and restoration of the data. The code described herein is compiled by Microsoft Visual C 6.0 under Windows 2000 Professional.