&1 涉及的函数

  • GetCurrentDirectory:返回当前进程的当前目录,并不一定返回你的exe应用程序的目录

   如果你在应用程序中调用了打开文件对话框,你选择了一个文件,那么,这个文件所在的目录就成了当前进程的当前目录了。

  • assign:是字符串赋值函数;
  • GetModuleFileName:获取当前进程已加载模块的文件的完整路径,该模块必须由当前进程加载。

   如果想要获取另一个已加载模块的文件路径,可以使用GetModuleFileNameEx函数。

  • append(const string &s,int pos,int n):把字符串s中从pos开始的n个字符连接到当前字符串的结尾
  • SetCurrentDirectory:切换当前进程的当前工作目录。

 

&2 源代码

char buf[1000];
GetCurrentDirectory(1000, buf); //得到当前工作路径 
cout << buf << endl;
char strModule[256];
GetModuleFileName(NULL, strModule, 256); //得到当前模块路径 
cout << strModule << endl;
string a;
a.assign(buf);
//cout << a << endl;
a.append("//..//"); //设置为当前工作路径为当时的上一级 
//a=a+"..//"; 
SetCurrentDirectory(a.c_str()); //设置 
GetCurrentDirectory(1000, buf);
cout << buf << endl;

 

&3 代码结果

主要有三个输出:

    • 第一个 buf,得到当前工作路径 ;
    • strModule,得到当前模块路径 ;
    • 第二个buf ,新的当前工作路径 ,之前工作路径的上一级。

相关文章:

  • 2021-11-23
  • 2021-11-23
  • 2021-11-23
  • 2021-10-20
  • 2021-09-08
  • 2021-05-23
猜你喜欢
  • 2021-11-23
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2021-07-03
  • 2021-11-23
相关资源
相似解决方案