visual-studio Visual Studio 中的相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/165338/
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
Relative paths in Visual Studio
提问by caschw
I'm working in Visual Studio 2005 and have added a text file that needs to be parsed by right-clicking the project in the solution explorer and add --> new item. This places the .txt file to the project folder. The debug .exe file is in the /bin/debug folder.
我在 Visual Studio 2005 中工作,并添加了一个文本文件,需要通过在解决方案资源管理器中右键单击项目并添加 --> 新项目来解析该文件。这会将 .txt 文件放置到项目文件夹中。调试 .exe 文件位于 /bin/debug 文件夹中。
How do I properly point to the txt file from code using relative paths that will properly resolve being two folders back, while also resolving to be in the same folder after the solution is published?
如何使用相对路径从代码正确指向 txt 文件,该路径将正确解析为两个文件夹,同时在解决方案发布后解析为同一文件夹?
采纳答案by Matt
Check out the Application Class. It has several members that can be used to locate files, etc. relative to the application once it's been installed.
查看应用程序类。它有几个成员,可用于在安装后定位与应用程序相关的文件等。
For example, Application.ExecutablePath tells you where the running EXE file is located; you could then use a relative path to find the file e.g. ..\..\FileToBeParsed.txt. However, this assumes that the files are deployed in the same folder structure as the project folder structure, which is not usually the case.
例如,Application.ExecutablePath 会告诉您正在运行的 EXE 文件所在的位置;然后,您可以使用相对路径来查找文件,例如 ..\..\FileToBeParsed.txt。但是,这假定文件部署在与项目文件夹结构相同的文件夹结构中,通常情况并非如此。
Consider properties like CommonAppDataPath and LocalUserAppDataPath to locate application-related files once a project has been deployed.
考虑 CommonAppDataPath 和 LocalUserAppDataPath 等属性,以便在项目部署后定位与应用程序相关的文件。
回答by David Kreps
If I understand your question correctly: Select the file in the "Solution Explorer". Under properties --> Copy to Output Directory, select "Copy Always" or "Copy if Newer". For build action, select "Content". This will copy the file to the /bin/debug folder on build. You should be able to reference it from the project root from then on.
如果我正确理解您的问题:在“解决方案资源管理器”中选择文件。在属性 --> 复制到输出目录下,选择“始终复制”或“如果较新则复制”。对于构建操作,选择“内容”。这会将文件复制到构建时的 /bin/debug 文件夹。从那时起,您应该能够从项目根目录中引用它。
回答by Seth Petry-Johnson
An alternative to locating the file on disk would be to include the file directly in the compiled assembly as an embedded resource. To do this, right-click on the file and choose "Embedded Resource" as the build action. You can then retrieve the file as a byte stream:
在磁盘上定位文件的另一种方法是将文件作为嵌入资源直接包含在已编译的程序集中。为此,请右键单击该文件并选择“嵌入式资源”作为构建操作。然后,您可以将文件作为字节流检索:
Assembly thisAssembly = Assembly.GetExecutingAssembly();
Stream stream = thisAssembly.GetManifestResourceStream("Namespace.Folder.Filename.Ext");
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
More information about embedded resources can be found hereand here.
If you're building an application for your own use, David Krep's suggestion is best: just copy the file to the output directory. If you're building an assembly that will get reused or distributed, then the embedded resource option is preferable because it will package everything in a single .dll.
如果您正在构建供自己使用的应用程序,David Krep 的建议是最好的:只需将文件复制到输出目录即可。如果您正在构建一个将被重用或分发的程序集,那么嵌入式资源选项是更可取的,因为它将所有内容打包在一个 .dll 中。
回答by Mitch Wheat
I would add a post build step that copies the txt file into the active configuration's output folder:
我会添加一个后期构建步骤,将 txt 文件复制到活动配置的输出文件夹中:
xcopy "$(ProjectDir)*.txt" "$(OutDir)"
There are a few macros defined such as "$(ConfigurationName)", $(ProjectDir)
定义了一些宏,例如 " $(ConfigurationName)",$(ProjectDir)
回答by Mitch Wheat
Go to Project Properties -> Configuration Properties -> Debugging
转到项目属性 -> 配置属性 -> 调试
Set "Working Directory" to $(TargetDir)
将“工作目录”设置为 $(TargetDir)
Then you can properly reference your file using a relative path in your code (e.g. "....\foo.xml")
然后您可以使用代码中的相对路径正确引用您的文件(例如“....\foo.xml”)
回答by Peter
The Application.ExecutablePath solution don't work for unittests. The path of vstesthost.exe will be returned
Application.ExecutablePath 解决方案不适用于单元测试。将返回 vstesthost.exe 的路径
System.Reflection.Assembly.GetExecutingAssembly().Location will work but gives the path with the assemblyname included.
System.Reflection.Assembly.GetExecutingAssembly().Location 将起作用,但提供包含程序集名称的路径。
So this works:System.Reflection.Assembly.GetExecutingAssembly().Location + "\..\" + "fileToRead.txt"
所以这有效:System.Reflection.Assembly.GetExecutingAssembly().Location + "\..\" + "fileToRead.txt"
But just :
只是 :
fileToRead.txt
Works also. Seems working directory is the executing assembly directory by default.
作品也。似乎工作目录默认是执行程序集目录。
回答by Aidan Ryan
You can add a post-build event to copy the .txt file to the build output folder. Then your code can assume that the file is in the same folder as the executable.
您可以添加构建后事件以将 .txt 文件复制到构建输出文件夹。然后您的代码可以假设该文件与可执行文件位于同一文件夹中。

