[英]how to reference file by path in published c# visual studio build
I have a file path which I access like this我有一个像这样访问的文件路径
string[] pathDirs = { Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\")),
"config", "file.txt" };
string pathToFile = Path.Combine(pathDirs);
When I run the build from within visual studio it gets the config
directory from the root directory of the project but when I publish the build and run the program from the published build I get the error当我从 Visual Studio 中运行构建时,它会从项目的根目录中获取
config
目录,但是当我发布构建并从已发布的构建运行程序时出现错误
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\<user>\AppData\Local\Apps\2.0\GA3VWRPE.G83\KDK3Q6QC.VP1\sv20..tion_333839f4362dc717_0001.0000_958d209d94853f42\config\file.txt'.
I'm unsure how to access this directory and file in the published build.我不确定如何在已发布的版本中访问此目录和文件。 How would I do this?
我该怎么做?
To get the path to the executable, you can use Assembly.GetExecutingAssembly().Location
, which could be incorporated into your code like:要获取可执行文件的路径,您可以使用
Assembly.GetExecutingAssembly().Location
,它可以合并到您的代码中,例如:
string filePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location), "..", "..", "config", "file.txt"));
Thanks to @ManiVI for that comment.感谢@ManiVI 的评论。
Once I followed these steps the published program managed to find file.txt.一旦我按照这些步骤操作,发布的程序就设法找到了 file.txt。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.