VC + + To Achieve folder time property acquisition and change

Source: Internet
Author: User
Tags function prototype local time win32 root directory backup

Introduction:

For most it enterprises and most individual users, data backup is primarily a simple backup of the data content, and rarely involves backing up the data itself, such as file attributes, the time attribute of the folder in which it resides. In some special industries that have strict requirements on data management, it is equally important to backup these related attribute information. However, under the current Windows operating system, whether directly by manual data backup or through some data backup software, it is difficult to keep this information completely intact copy, especially for every moment in the change of time attribute is difficult to maintain its original information. Not only that, there are no tools or means to directly modify such attributes under the Windows operating system. To this end, the author has done research on this problem, and summed up a set of relatively simple solutions. Among them, for the file properties of the backup and change methods the author has been in the computer newspaper Tenkine "in VC + + on the acquisition and change of file properties," a text made an exposition, therefore, this article is no longer to repeat, but focus on the folder time attributes of the acquisition and change methods to introduce.

Design ideas

The Windows operating system does not provide any method or means to modify the folder time properties, even in the Win32 API function, but only provides a function call to modify the file time property, without a single word about the modification of the folder time attribute. Although Windows provides a backup program that replicates the time attributes of all subfolders under the backup folder as-is, it does not maintain a constant root-time attribute. As a result, you might consider taking a backup approach and starting with the Win32 API functions associated with backup. Specifically, you can open the folder by opening the file, and then you can use the Win32 API functions that were originally used to process the file Time Properties Getfiletime () and Setfiletime () to get the original time properties and set the Backup folder time properties as parameters. , such a process ensures that the folder has a consistent time attribute before and after the backup.

As you can see from the previous analysis, opening a folder in the way you open it is the key to the entire process, usually the Win32 API function that is used primarily to create and open files CreateFile () is not only used to create and open file objects, but it can also be used to create, open pipes, post slots, Communication resources, disk drives (for Windows NT only), consoles and folders (open only), and so on. The following is a prototype of CreateFile ():

HANDLE CreateFile( LPCTSTR lpFileName, // 文件名 指针
 DWORD dwDesiredAccess, // 访问模式
 DWORD dwShareMode, // 共 享模式
 LPSECURITY_ATTRIBUTES lpSecurityAttributes, //安全属性
  DWORD dwCreationDisposition, // 创建方式
 DWORD dwFlagsAndAttributes, // 文件属性
 HANDLE hTemplateFile // 指向待复制属性的文件指针
);

When you use it for a folder open operation, the first parameter lpfilename should be set to the name of the folder you want to open, and the access mode can be set flexibly as needed, and for this article, only read from the source folder can be set to Generic_read. For a backed-up folder, you need to have generic_write support because the property information needs to be written to it; the dwShareMode settings for shared-mode parameters do not differ from the settings for file processing, which can be set to file_share_read| File_share_delete because the CreateFile () function cannot be created as a folder operation, the creation mode can only open objects that already exist, that is, dwcreationdisposition should be set to Open_ EXISTING, the setting of the dwFlagsAndAttributes parameter is more important than the CreateFile () by setting the parameter to the File_flag_backup_semantics property. function to open a folder.

Typically, the time attribute of a file or folder refers to several specific attributes, such as creation time, recent access time, and recent modification time. The above properties for a file can be obtained by Getfiletime (), and for a folder, the resulting handle can be used as a file handle when it is opened through the CreateFile () function. Therefore, the time attribute of the folder can also be obtained by the Getfiletime () function. The Getfiletime () function prototype is as follows:

BOOL GetFileTime( HANDLE hFile, // 文件句柄
 LPFILETIME lpCreationTime, // 创建时间的地址
 LPFILETIME lpLastAccessTime, // 最近 访问时间的地址
 LPFILETIME lpLastWriteTime // 最近修改时间的地址
);

The latter three parameters are pointers to the FILETIME structure and are all UTC time, and can be converted to local time by the FileTimeToLocalFileTime () function, if necessary. Further, the FileTimeToSystemTime () function can be used to convert it from file time format to system time format, and the transformed time format will be stored in a SYSTEMTIME structure object. Similarly, when you write time information to a folder property, if it is not a file time format, you should convert it from the system time format to the file time format by using the SystemTimeToFileTime () function, and then write the specified time through the Setfiletime () function To the time attribute in the folder. In this way, all folders, including the root directory, can maintain consistent time attributes during data backup and recovery.

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.