VC ++ obtains and modifies the folder time attribute (reproduced). Introduction:
For most IT enterprises and most individual users, data backup is mainly a simple backup of data content, and seldom involves the attributes of backup data, suchFile AttributesAnd the time attribute of the folder. In some special industries that have strict requirements on data management, it is equally important to back up these attributes. However, in the current Windows operating system, it is difficult to completely copy this information, whether it is directly performed through manual data backup or some data backup software, in particular, it is difficult to maintain the original information of time attributes that are changing every moment. In addition, Windows does not even provide tools and means to directly modify such attributes. To this end, I have studied this problem and summarized a simple solution. ForFile AttributesThe author has reportedVC++File AttributesSo this article will not go into details here, but will focus on getting and changing folder time attributes.
Design Concept
In Windows, no methods or methods are provided to modify the folder time attribute, even in Win32 API functions, it only provides function calls for modifying the time attribute of a file, but there is no phrase for modifying the time attribute of a folder. Although the backup program provided by Windows can completely copy the Time Attributes of all subfolders in the folder to be backed up, it cannot keep the root directory time attribute constant. Therefore, we can consider using backup methods and start with Win32 API functions related to backup. Specifically, you can open the folder by opening the file, and then use the Win32 API functions getfiletime () and setfiletime () originally used to process the file time attribute () to obtain the original time attribute and set the folder time attribute after backup with its parameter. This process ensures that the folder time attribute is consistent before and after backup.
According to the previous analysis, it can be seen that opening a folder by opening a file is the key to the entire process. It is usually used to create and open a file using the Win32 API function createfile () it is not only used to create and open file objects. In fact, it can also be used to create and open pipelines, mail slots, communication resources, and disk drives (for Windows NT only) console and folder (can only be opened. The following is a prototype of createfile:
Handle createfile (lpctstr lpfilename, // File Name Pointer
DWORD dwdesiredaccess, // Access Mode
DWORD dw1_mode, // share mode
Lpsecurity_attributes lpsecurityattributes, // Security Attribute
DWORD dwcreationdisposition, // Creation Method
DWORD dwflagsandattributes ,//File Attributes
Handle htemplatefile // pointer to the file to be copied
);
When you use it to open a folder, the first parameter lpfilename should be set to the name of the folder to be opened. The access mode can be set as needed. For this article, the source folder can only be read, so it can be set to generic_read. For the backup folder, because attribute information needs to be written, generic_write must be supported; there is no difference between the setting of the Shared Mode Parameter dw1_mode and the setting during file processing. Here we can set it to file_1__read | file_1__delete; because the createfile () function is used for folder operations, it cannot be created. Therefore, the Creation method can only open existing objects, that is, dwcreationdisposition should be set to open_existing. In contrast, it is important to set the dwflagsandattributes parameter, creat is created by setting this parameter to the file_flag_backup_semantics attribute. The efile () function to open a folder.
Generally, the time attribute of files and folders refers to the creation time, recent access time, and recent modification time. The preceding attributes of a file can be obtained through getfiletime (). For folders, after opening the folder using the createfile () function, the obtained handle can be used as a file handle. Therefore, you can use the getfiletime () function to obtain the time attribute of the folder. The getfiletime () function prototype is as follows:
Bool getfiletime (handle hfile, // file handle
Lpfiletime lpcreationtime, // address of the Creation Time
Lpfiletime lplastaccesstime, // address of the last access time
Lpfiletime lplastwritetime // address of the last modification time
);
The last three parameters all point to the filetime structure and get the UTC time. If necessary, you can use the filetimetolocalfiletime () function to convert the UTC time to the local time. In addition, you can use the filetimetosystemtime () function to convert the file time format to the system time format. The converted time format is saved in a systemtime structure object. Similarly, when writing time information to the folder attribute, if it is not the file time format, you should also use the systemtimetofiletime () function to convert it from the system time format to the file time format, then, use the setfiletime () function to write the specified time to the time attribute of the folder. In this way, all folders, including the root directory, can maintain the same time attribute during data backup and recovery.
Simple Example
The following is a simple application example based on the previous descriptions, in this example, the time attribute information can be read from the specified folder and written back after modification (only the last modification time is processed here, other time attributes can be implemented in a similar way ). Here, two functions getdirtime () and setdirtime () are used to obtain and change the folder time information. The following describes the implementation process of these two functions in the form of Annotations:
// Obtain the time attribute of the specified folder. The entry parameter dirname specifies the folder to be processed, and stime is
// Pointer to the systemtime Structure
Bool csetfordertimedlg: getdirtime (cstring dirname, systemtime & stime ){
// Open a folder
Handle hdir = createfile (dirname, generic_read,
File_cmd_read | file_cmd_delete,
Null, open_existing,
File_flag_backup_semantics, null );
Filetime lpcreationtime; // Folder creation time
Filetime lplastaccesstime; // The last access time to the folder
Filetime lplastwritetime; // The last modification time of the folder.
//Get FileFolder time attribute information
If (getfiletime (hdir, & lpcreationtime, & lplastaccesstime, & lplastwritetime )){
Filetime ftime;
Filetimetolocalfiletime (& lplastwritetime, & ftime); // The local time for conversion
Filetimetosystemtime (& ftime, & stime); // convert to system time format
}
Closehandle (hdir); // close the opened folder
Return retval;
}
// Set the time attribute of the specified folder. The entry parameter dirname specifies the folder to be processed, new_time
// A pointer to the systemtime Structure
Bool csetfordertimedlg: setdirtime (cstring dirname, systemtime new_stime ){
// Open the Win32 API call of the Directory
Handle hdir = createfile (dirname, generic_read | generic_write,
File_cmd_read | file_cmd_delete,
Null, open_existing,
File_flag_backup_semantics, null );
Filetime lpcreationtime; // Folder creation time
Filetime lplastaccesstime; // The last access time to the folder
Filetime lplastwritetime; // The last modification time of the folder.
Systemtimetofiletime (& new_stime, & lpcreationtime); // convert to the file time format
Systemtimetofiletime (& new_stime, & lplastaccesstime );
Systemtimetofiletime (& new_stime, & lplastwritetime );
// Set the time attribute of the folder
Bool retval = setfiletime (hdir, & lpcreationtime, & lplastaccesstime, & lplastwritetime );
Closehandle (hdir); // close the folder
Return retval;
}
At this point, it is convenient to call the getdirtime () and setdirtime () functions to obtain and set the time attribute of any specified folder, specifically:
Systemtime stime; // System Time Structure object
If (getdirtime (m_path, stime ))
{
// IfGet FileThe time attribute is successfully clipped. The obtained time information is saved in the stime structure object.
......
// If necessary, you can modify the obtained time attribute or keep it unchanged.
......
// Write the modified time attribute back to the folder
Setdirtime (m_path, stime );
}
Summary
This article uses the createfile () function to open a folder and treat it as a file in future processing. Therefore, you can use getfiletime (), setfiletime () to obtain and write the time attribute. You can set the time attribute for any folder including the root directory. It has good application prospects in data backup and restoration. The code described in this article is compiled by Microsoft Visual C ++ 2000 under Windows 6.0 professional.