Windows API一日一练(52)GetCurrentDirectory和SetCurrentDirectory函数

在开发软件里,常常碰到要读取当前目录下的配置参数文件,或者打开当前目录下别的程序来运行,那么就需要获取当前进程的目录位置,这就需要使用函数 GetCurrentDirectory 获取当前进程所有在的目录。同时也可以使用 SetCurrentDirectory 函数来改变进程的当前目录。
函数 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 GetCurrentDirectoryGetCurrentDirectoryW
#else
#define GetCurrentDirectoryGetCurrentDirectoryA
#endif // !UNICODE
WINBASEAPI
BOOL
WINAPI
SetCurrentDirectoryA(
__in LPCSTR lpPathName
);
WINBASEAPI
BOOL
WINAPI
SetCurrentDirectoryW(
__in LPCWSTR lpPathName
);
#ifdef UNICODE
#define SetCurrentDirectorySetCurrentDirectoryW
#else
#define SetCurrentDirectorySetCurrentDirectoryA
#endif // !UNICODE
nBufferLength 是缓冲区的大小。
lpBuffer 是接收目录的缓冲区指针。
lpPathName 是设置的目录。
调用函数的例子如下:
#001// 获取或者改变当前目录路径。
#002// 蔡军生 2007/10/17 QQ:9073204 深圳
#003void GetCurDir(void)
#004{
#005 //
#006 TCHAR szBuf[MAX_PATH];
#007 ZeroMemory(szBuf,MAX_PATH);
#008 if (GetCurrentDirectory(MAX_PATH,szBuf) > 0)
#009 {
#010 // 获取进程目录成功。
#011 OutputDebugString(szBuf);
#012 }
#013 else
#014 {
#015 // 改变当前目录位置。
#016 SetCurrentDirectory(_T("C://"));
#017 }
#018
#019 OutputDebugString(_T("/r/n"));
#020}
#021
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值