更改当前工作目录 VS13? [英] Change current working directory VS13?

查看:41
本文介绍了更改当前工作目录 VS13?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如本发布中所述,当我调试我的 SDL 程序是相对于 .vcproj 而不是 .exe(它应该是 IMO)

As stated in this post the working directory when I debug my SDL program is relative to the .vcproj instead of the .exe (which it should be IMO)

所以我想知道我是否可以更改它,所以当我按 F5 时,路径将相对于 .exe 而不是 .vcproj?我正在使用的当前(相对简单)的解决方法是简单地打开文件夹并从那里启动程序,但我更喜欢简单地按 F5.

So I'm wondering if there's anyway I can change this, so when i press F5 the path will be relative to the .exe and not .vcproj? The current (relatively easy) workaround I'm using, is simply opening up the folder and starting program from there, but I would much rather prefer simply pressing F5.

推荐答案

对您问题的天真的答案是您可以在 Debugging 配置中设置 Working Directory 选项$(TargetDir) 的属性.默认设置为 $(ProjectDir) 并且默认情况下项目目录不是可执行文件的输出位置.但是,我不建议您选择此选项,如下所述.

The naive answer to your question is that you can set the Working Directory option in the Debugging configuration properties to $(TargetDir). The default setting is $(ProjectDir) and by default the project directory is not where the executable file is output. However, I do not recommend you take this option, as I explain below.

您试图以错误的方式解决此问题.您的根本问题是您假设工作目录与包含可执行文件的目录相同.没有理由这样,你不应该依赖它.您知道这些文件与可执行文件位于同一目录中,因此您应该在那里查看,而不是工作目录,巧合的是,它有时与可执行目录相同.

You are attempting to solve this the wrong way. Your fundamental problem is that you are assuming that the working directory is the same as directory which contains the executable file. There's no reason for that to be so, and you should not rely one it. You know that the files are in the same directory as the executable and so you should look there, rather than the working directory which is only sometimes, coincidentally, the same as the executable directory.

因此,不要依赖于根据您的喜好设置工作目录的进程调用者,而是让您的程序独立于工作目录.您声明您希望使用您知道相对于可执行目录的位置的文件.所以有答案.使用包含可执行文件的目录作为基础构建文件的完整路径.

So, instead of relying on the invoker of the process setting up the working directory to your liking, make your program independent of the working directory. You state that you wish to work with files whose location you know relative to the executable directory. So there is the answer. Construct full paths to your files, using the directory which contains the executable as the base.

如果您需要找出可执行文件的位置,请调用GetModuleFileName(),并去掉文件名.您剩下的是包含可执行文件的目录.将其与文件的相对路径相结合,您的代码现在独立于工作目录.

If you need to find out the location of the executable, call GetModuleFileName(), and strip off the file name. What you have left is the directory which contains your executable. Combine that with the relative path of your files and your code is now independent from the working directory.

这篇关于更改当前工作目录 VS13?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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