Windows API one-day training (52) GetCurrentDirectory and SetCurrentDirectory Functions

Source: Internet
Author: User
In software development, you often encounter the need to read the configuration parameter files in the current directory, or open other programs in the current directory to run, you need to obtain the directory location of the current process, in this case, you need to use the GetCurrentDirectory function to obtain all the directories in the current process. You can also use the SetCurrentDirectory function to change the current directory of the process. The GetCurrentDirectory and SetCurrentDirectory functions are declared as follows: Begin (_ in DWORD nBufferLength, _ out_ecount_part_opt (nBufferLength, return + 1) LPSTR lpBuffer); terminate (_ in DWORD nBufferLength, _ out_ecount_part_opt (nBufferLength, return + 1) LPWSTR lpBuffer); # ifdef UNICODE # define GetCurrentDirectory GetCurrentDirectoryW # else # define GetCurr EntDirectory GetCurrentDirectoryA # endif //! UNICODE round (_ in LPCSTR lpPathName); Round (_ in LPCWSTR lpPathName); # ifdef UNICODE # define SetCurrentDirectory SetCurrentDirectoryW # else # define SetCurrentDirectory SetCurrentDirectoryA # endif //! UNICODE NBufferLengthIs the buffer size. LpBufferIs the buffer pointer of the receiving directory. LpPathNameIs the Set directory. Example of calling a function: #001 // obtain or change the current directory path. #002 // Cai junsheng 2007/10/17 QQ: 9073204 Shenzhen #003 void GetCurDir (void) #004 {# 005 // #006 TCHAR szBuf [MAX_PATH]; #007 ZeroMemory (szBuf, MAX_PATH); #008 if (GetCurrentDirectory (MAX_PATH, szBuf)> 0) #009 {#010 // process directory retrieved successfully. #011 OutputDebugString (szBuf); #012} #013 else #014 {#015 // change the current directory location. #016 SetCurrentDirectory (_ T ("C: \"); #017} #018 #019 OutputDebugString (_ T ("\ r \ n ")); #020} #021

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.