您的位置:首页 > 其它

获取exe程序路径

2016-09-10 09:04 120 查看
char exePath[LEN_PATH]={0};

GetModuleFileName(NULL,exePath,LEN_PATH); // 得到exe文件的绝对路径

strrchr( exePath, '\\')[0]= '\0';
printf("exePath: %s\n",exePath);

exePath 为程序入口的路径

比如test1:

int main()

{

    FILE *fd = fopen("test1.txt","w");

    fclose(fd);

    char exePath[LEN_PATH]={0};

    GetModuleFileName(NULL,exePath,LEN_PATH); // 得到exe文件的绝对路径

    strrchr( exePath, '\\')[0]= '\0';

    printf("exePath: %s\n",exePath);

    return 0;

}

test2:

int main()

{

    int x;

    x = system("test1.exe");

    printf("x:%d\n",x);

    system("pause");

    return 0;

}

运行test2.exe得到如下结果,获得的是test2.exe路径,不是test1.exe
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows C