我如何将工作目录设置为 C++ 中的“解决方案目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4815423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:37:59  来源:igfitidea点击:

How do i set the working directory to the "solution directory" in c++

c++visual-studio

提问by lital maatuk

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 file and the current directory changes in the middle of the code. i want to change it back.

编辑:我正在尝试读取一个文件,当前目录在代码中间发生变化。我想把它改回来。

采纳答案by LiMuBei

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.

如果您的当前目录正在更改,您可能应该在启动时将您的工作目录保存在某个变量中,您可以稍后访问以将 cwd 设置回那里。至少我是这样理解你的问题的。

For getting the cwd, thismight help.

为了获得 cwd,可能会有所帮助。

回答by rturrado

In Visual Studio 2010:

在 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)\.
  1. 转到项目属性(在解决方案资源管理器中右键单击项目名称,然后在弹出菜单上单击属性)。
  2. 然后,在配置属性/调试下,将工作目录设置为$(SolutionDir)$(Configuration)\.


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

可用宏的完整列表(在 docs.microsoft.com 上):MSBuild 命令和属性的常用宏

回答by KitsuneYMG

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

您可以使用 posix 子系统 ( <direct.h>) 并访问功能

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

_getcwd()/_wgetcwd()获取当前工作目录
_chdir()/_wchdir()设置当前工作目录

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 getcwdand chdir(w/o the leading underscore).

并使用getcwdchdir(不带前导下划线)。

回答by evandrix

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

您是否尝试过使用环境变量 $(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.

另外,希望 VS 的版本无关紧要,但是这个答案是基于平台是 VS2005 的假设提供的。

Hope this helps.

希望这可以帮助。

回答by Vlad Kaponir

  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 appuse Windows API function GetCurrentDirectorythat retrieves path to your processdirectory. Compiled code may live independently of project therefore it make sense to refer path to data files relatively to process (exe).

  1. 对于 Visual Studio 目的(包括标题等),您可以使用此处列出的 $(ProjectDir) 或 $(SolutionDir) 之类的VS 宏列表

  2. 要读取应用程序代码中的文件,请使用 Windows API 函数GetCurrentDirectory来检索进程目录的路径。编译后的代码可能独立于项目而存在,因此相对于进程 (exe) 引用数据文件的路径是有意义的。

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

似乎问题的作者询问了第二项,而其他人则回答了第一项。