在開(kāi)發(fā)軟件里,常常碰到要讀取當(dāng)前目錄下的配置參數(shù)文件,或者打開(kāi)當(dāng)前目錄下別的程序來(lái)運(yùn)行,那么就需要獲取當(dāng)前進(jìn)程的目錄位置,這就需要使用函數(shù)
GetCurrentDirectory
獲取當(dāng)前進(jìn)程所有在的目錄。同時(shí)也可以使用
SetCurrentDirectory
函數(shù)來(lái)改變進(jìn)程的當(dāng)前目錄。
?
函數(shù)
GetCurrentDirectory
和
SetCurrentDirectory
聲明如下:
?
WINBASEAPI
DWORD
WINAPI
GetCurrentDirectoryA(
??? __in DWORD nBufferLength,
??? __out_ecount_part_opt(nBufferLength, return + 1) LPSTR lpBuffer
??? );
WINBASEAPI
DWORD
WINAPI
GetCurrentDirectoryW(
??? __in DWORD nBufferLength,
??? __out_ecount_part_opt(nBufferLength, return + 1) LPWSTR lpBuffer
??? );
#ifdef UNICODE
#define GetCurrentDirectory?GetCurrentDirectoryW
#else
#define GetCurrentDirectory?GetCurrentDirectoryA
#endif // !UNICODE
?
WINBASEAPI
BOOL
WINAPI
SetCurrentDirectoryA(
??? __in LPCSTR lpPathName
??? );
WINBASEAPI
BOOL
WINAPI
SetCurrentDirectoryW(
??? __in LPCWSTR lpPathName
??? );
#ifdef UNICODE
#define SetCurrentDirectory?SetCurrentDirectoryW
#else
#define SetCurrentDirectory?SetCurrentDirectoryA
#endif // !UNICODE
?
nBufferLength
是緩沖區(qū)的大小。
lpBuffer
是接收目錄的緩沖區(qū)指針。
lpPathName
是設(shè)置的目錄。
?
調(diào)用函數(shù)的例子如下:
#001??//
獲取或者改變當(dāng)前目錄路徑。
#002??//
蔡軍生
?2007/10/17 QQ:9073204
深圳
#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?????????????? //
獲取進(jìn)程目錄成功。
#011?????????????? OutputDebugString(szBuf);
#012???????? }
#013???????? else
#014???????? {
#015?????????????? //
改變當(dāng)前目錄位置。
#016?????????????? SetCurrentDirectory(_T("C:\\"));
#017???????? }
#018?
#019???????? OutputDebugString(_T("\r\n"));??????????
#020??}
#021?
?
?
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1829984
Windows API一日一練(52)GetCurrentDirectory和SetCurrentDirectory函數(shù)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
