C ++ [Windows]可执行文件所在文件夹的路径 [英] C++ [Windows] Path to the folder where the executable is located

查看:197
本文介绍了C ++ [Windows]可执行文件所在文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的C ++应用程序在Windows上访问一些文件与 fstream 。这些文件都位于我的exe文件所在的文件夹的子文件夹中。




  • 最简单和更重要的是:

    使用> http://msdn.microsoft.com/en-us/library/ms683199%28VS.85%29.aspx\"> GetModuleHandle GetModuleFileName ,以了解您的exe在哪里运行。

      HMODULE hModule = GetModuleHandleW(NULL); 
    WCHAR路径[MAX_PATH];
    GetModuleFileNameW(hModule,path,MAX_PATH);

    然后从路径中删除exe名称。


    I need to access some files with fstream in my C++ app on Windows. Those files are all located in subfolders of the folder where my exe file is located.

    • What is the easiest and more important: safest way to get the path to the folder of the current executable?

    解决方案

    Use GetModuleHandle and GetModuleFileName to find out where your exe is running from.

    HMODULE hModule = GetModuleHandleW(NULL);
    WCHAR path[MAX_PATH];
    GetModuleFileNameW(hModule, path, MAX_PATH);
    

    Then strip the exe name from path.

    这篇关于C ++ [Windows]可执行文件所在文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆