.net 在 Visual Studio 中调试时如何更改工作目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1102976/
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
How do I change the working directory while debugging in Visual Studio?
提问by Riri
I have a Windows Formsproject in Visual Studio that reads configuration files from another project in the same solution. Once deployed, this application will run from the root folder for the solution, and the files will then be read from something like "<Executing assembly path>/Project1/connection.config".
我在 Visual Studio 中有一个Windows 窗体项目,它从同一解决方案中的另一个项目读取配置文件。部署后,此应用程序将从解决方案的根文件夹运行,然后将从“<执行程序集路径>/Project1/connection.config”之类的内容中读取文件。
When I however debug the Windows Forms application and get the executing assembly path, it will run from "bin/Debug" or "bin/Release", ergo the path to the configuration files is different.
然而,当我调试 Windows 窗体应用程序并获取执行程序集路径时,它将从“bin/Debug”或“bin/Release”运行,因此配置文件的路径不同。
Is there a way to tell Visual Studio either debug the application from a different path or just copy the application once compiled and debug/run it from the same path as it will be run from once it is deployed?
有没有办法告诉 Visual Studio 从不同的路径调试应用程序,或者在编译后复制应用程序并从与部署后运行的路径相同的路径调试/运行它?
采纳答案by Russell Troywest
Set a post build event on your project to move the configuration file into the debug build directory.
在您的项目上设置构建后事件以将配置文件移动到调试构建目录中。
Right click on the project that will require the configuration from a different project and select properties, click on the build events tab, and add a post build event. Something like this:
右键单击需要来自不同项目的配置的项目并选择属性,单击构建事件选项卡,然后添加构建后事件。像这样的东西:
Copy configFilePath $(TargetPath)
回答by Timbo
In the project properties, open the debug tab. In there, you will find a textbox to change the working directory.
在项目属性中,打开调试选项卡。在那里,您会找到一个用于更改工作目录的文本框。
回答by Jason
I solved this problem by going to the project properties compile tab, and changing the build output path. Hopefully this will work for you.
我通过转到项目属性编译选项卡并更改构建输出路径解决了这个问题。希望这对你有用。
I believe you will need to change that back to bin\ when you are ready to do a release build.
我相信当您准备好进行发布构建时,您需要将其改回 bin\。
回答by Genom
The reason, why it writes back to the same old folder may be administration rights.
原因,为什么它写回同一个旧文件夹可能是管理权限。

