How do I set the working directory to the “solution directory” in c++?

后端 未结 5 1164
生来不讨喜
生来不讨喜 2020-12-30 02:01

I want to set the current directory to the solution diretory/configuration name. How do I do that? Can I use the global variables somehow?

Edit: I am trying to read a

相关标签:
5条回答
  • 2020-12-30 02:13

    If your current directory is changing, you should probably save your working directory at startup in some variable you can access later to set cwd back there. At least this is how I understand your question.

    For getting the cwd, this might help.

    0 讨论(0)
  • 2020-12-30 02:17

    You can use the posix subsystem ( <direct.h> ) and access the functions

    _getcwd()/_wgetcwd() Gets the current working directory
    _chdir()/_wchdir() Sets the current working directory

    If you need your code to be cross platform, you can do the following:

    #ifdef _WIN32
    #  include <direct.h>
    #  define getcwd _getcwd
    #  define chdir _chrdir
    #else
    #  include <unistd.h>
    #endif
    

    and use getcwd and chdir (w/o the leading underscore).

    0 讨论(0)
  • 2020-12-30 02:27
    1. For Visual Studio purposes (include header, etc) you can use macro like $(ProjectDir) or $(SolutionDir) listed here: VS macro list

    2. To read files in code of your app use Windows API function GetCurrentDirectory that retrieves path to your process directory. Compiled code may live independently of project therefore it make sense to refer path to data files relatively to process (exe).

    It seems author of question asks about 2-nd item and others answer to 1-st item.

    0 讨论(0)
  • 2020-12-30 02:32

    Have you tried using the environment variable $(SolutionDir) ?

    With reference to this thread here.

    Also, hopefully the version of VS does not matter, but this answer is furnished based on the assumption that the platform is VS2005.

    Hope this helps.

    0 讨论(0)
  • 2020-12-30 02:35

    In Visual Studio 2010:

    1. Go to the project properties (rigth click on the project name in the Solution Explorer, then Properties on the pop up menu).
    2. Then, under Configuration Properties / Debugging, set Working Directory to $(SolutionDir)$(Configuration)\.

    Full list of available macros (on docs.microsoft.com) : Common macros for MSBuild commands and properties

    0 讨论(0)
提交回复
热议问题