#include <iostream>
#include <Windows.h>
#include <tchar.h>
using std::cout;
using std::endl;
int main()
{
char buf[MAX_PATH];
DWORD dwRet;
dwRet = GetCurrentDirectoryA(MAX_PATH,buf);
// 返回0表示得到文件的当路径失败,当前路径就是dsw所在的文件夹
if(0 == dwRet)
{
cout << "GetCurrentDirectory failed :" << dwRet << endl;
return 1;
}
if(dwRet > MAX_PATH)
{
cout << "GetCurrentDirectory failed(buf too small,need "
<< dwRet << " chars)" << endl;
return 2;
}
cout << "GetCurrentDirectory is :" << endl;
cout << buf << endl;
cout << "realSize :" << dwRet << endl;
cout << endl;
char oldPath[MAX_PATH];
strcpy(oldPath,buf);
// 设置当前路径为c盘
// SetCurrentDirectoryA("..") // 就是设置目录为当前目录的上级目录
SetCurrentDirectoryA("C:\\");
dwRet = GetCurrentDirectoryA(MAX_PATH,buf);
cout << "GetCurrentDirectory is :" << endl;
cout << buf << endl;
cout << "realSize :" << dwRet << endl;
SetCurrentDirectoryA(oldPath);
cout << endl;
dwRet = GetCurrentDirectoryA(MAX_PATH,buf);
cout << "GetCurrentDirectory is :" << endl;
cout << buf << endl;
cout << "realSize :" << dwRet << endl;
cout << endl;
GetModuleFileNameA(NULL,buf,MAX_PATH);
cout << "Current module path : " << endl;
cout << buf << endl;
cout << endl;
system("pause");
return 0;
}
GetCurrentDirectoryA and SetCurrentDirectoryA
最新推荐文章于 2022-08-06 09:22:07 发布