One-day training of Windows APIs (51) createdirectory and removedirectory Functions

Source: Internet
Author: User

In the age of information explosion, classification of various types of information is already a very necessary function. For example, to save stock market data to a hard disk, you need to separate several directories for storage. For example, when you write logs to the hard disk, You need to divide them into several directories for storage, so that the maintenance staff can find the error information or useful information. For example, when I develop a bank's credit card system, I need to generate all the data transmitted over the network and save the text files to the directory of the day, that is, a directory needs to be created every day, store the logs generated from all transactions. How do you create a directory to meet this requirement? Therefore, you need to use the Windows API function createdirectory to create a directory. Of course, when there are too many directories, you need to call the removedirectory function to delete unnecessary directories. However, the removedirectory function can only delete empty directories, that is, files and subdirectories under the directory can be deleted.

The createdirectory and removedirectory functions are declared as follows:

Winbaseapi
Bool
Winapi
Createdirectorya (
_ In lpcstr lppathname,
_ In_opt lpsecurity_attributes lpsecurityattributes
);
Winbaseapi
Bool
Winapi
Createdirectoryw (
_ In lpcwstr lppathname,
_ In_opt lpsecurity_attributes lpsecurityattributes
);
# Ifdef Unicode
# Define createdirectory createdirectoryw
# Else
# Define createdirectory createdirectorya
# Endif //! Unicode

Winbaseapi
Bool
Winapi
Removedirectorya (
_ In lpcstr lppathname
);
Winbaseapi
Bool
Winapi
Removedirectoryw (
_ In lpcwstr lppathname
);
# Ifdef Unicode
# Define removedirectory removedirectoryw
# Else
# Define removedirectory removedirectorya
# Endif //! Unicode

Lppathname is the directory path.
Lpsecurityattributes is the Security Attribute of the directory.
The returned value is whether the execution is successful.

An example of calling a function is as follows:
#001 // create a directory.
#002 // Cai junsheng 2007/10/16 QQ: 9073204 Shenzhen
#003 STD: wstring strdir (_ T ("C: \ log "));
#004 if (! Createdirectory (strdir. c_str (), null ))
#005 {
#006 outputdebugstring (_ T ("the directory is not created successfully \ r \ n "));
#007
#008}
#009 else
#010 {
#011 // delete an empty directory.
#012 removedirectory (strdir. c_str ());
#013}
#014

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/caimouse/archive/2007/10/16/1828185.aspx

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.