简体   繁体   English

如何在已发布的 c# Visual Studio 构建中按路径引用文件

[英]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 的评论。

  1. Go to Solution Explorer and click Show All Files Go 到解决方案资源管理器并单击显示所有文件
  2. Go to file.txt, right-click and select Include In Project Go 到 file.txt,右键单击 select 包含在项目中
  3. in Properties在属性
  4. Build Action: Content,构建动作:内容,
  5. Copy To Output Directory: Copy always复制到 Output 目录:始终复制

Once I followed these steps the published program managed to find file.txt.一旦我按照这些步骤操作,发布的程序就设法找到了 file.txt。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在C#visual studio中获取Microsoft.SqlServer.Management.Dac的文件路径以供参考 - Can't get file path of Microsoft.SqlServer.Management.Dac in C# visual studio for reference Visual Studio:在C#程序中插入对可执行路径的引用 - Visual Studio: Inserting reference to executable path within C# program Visual Studio(C#)使用环境变量生成输出路径 - Visual Studio (C#) Build Output path using environmental variables 参考文件如何与构建操作内容一起发布 - how reference file published with build action content C#Visual Studio用户输入的文件路径和文件名 - C# visual studio user input for file path and file name 当从Visual Studio(而不是ClickOnce发布版本)运行C#WPF应用程序时,PowerPoint文件打开 - PowerPoint file opens when C# WPF app is running from Visual Studio, but not from ClickOnce published version 一旦我压缩文件(它们从 Visual Studio 中的文件路径引用打开),如何让我的应用程序从单独的 Winform 打开? (C#) - How do I get my app to open from a separate Winform once I ZIP the files (they open from a file path reference in Visual Studio)? (C#) 在c#,visual studio 2013中使用数据库文件的相对路径 - Using a relative path for a database file in c#, visual studio 2013 在 Visual Studio 2012 中的 C# 项目中更改文件完整路径 - Change file full path in a C# project in Visual studio 2012 Visual Studio 2013 C#构建输出到文件 - Visual Studio 2013 C# Build Output to File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM