所以我正在寻找一段代码,允许我搜索正在执行的文件的路径.例如,我正在做一个用于pendrives的自动运行程序(例子),但我不知道是否它最终将以D:,F:,G:或其他任何方式结束,因此程序将搜索它自己的路径,并根据使用某些"if"语句找到的路径打开另一个文件.
这就是我的想法:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main () {
// Insert 'search path' code and needed variables here.
if (-ThePath- == "d:\\AutoRun.exe")
{
system ("d:\\MyFolder\\OtherProgram.exe");
}
else if (-ThePath- == "f:\\AutoRun.exe")
{
system ("f:\\MyFolder\\OtherProgram.exe");
}
else if (-ThePath- == "g:\\AutoRun.exe")
{
system ("g:\\MyFolder\\OtherProgram.exe");
}
else
{
cout << "An error ocurred.\n";
cout << "Press enter to exit...\n";
cin.get();
};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以做到这一点?
GetModuleFileName
:这里的文档
EDITED - Pedro,微软的示例代码处理了很多事情.要获取文件路径,您只需要:
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) ) {
// handle error in GetModuleFileName
} else {
// now, szPath contains file path
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
174 次 |
最近记录: |