简体   繁体   English

从 C# 中的相同 package 获取位于 NuGet 包目录中的文件路径

[英]Get path of file located in NuGet package's directory from same package in C#

I have a .NET Framework (4.7) solution which is referencing a NuGet package.我有一个.NET Framework (4.7)解决方案,它引用了 NuGet package。 This NuGet package should read from an html and an xml file.此 NuGet package 应从htmlxml文件读取。 These files are both located within the packages' folder (NuGet package) and they are NOT needed by the solution directly, but only by the NuGet package itself.这些文件都位于包的文件夹(NuGet 包)中,解决方案不需要它们,而只有 NuGet package 本身需要它们。

How can I refer to these files when running the solution that is consuming the mentioned NuGet package?在运行使用上述 NuGet package 的解决方案时,如何参考这些文件?

I tried with ./ but all this does is point to the root directory of my solution instead.我尝试使用./但这一切只是指向我的解决方案的根目录。

The actual call that I am trying is: File.ReadAllText(path) .我正在尝试的实际调用是: File.ReadAllText(path)

I also tried to set Copy to Output Directory property of the files to Copy always and use Build Action as Content .我还尝试将Copy to Output Directory属性以Copy always并使用Build Action as Content This is copying these files in the packages/<package name>/Content/*.html for example and somehow I guess I can get this path, such as the solution given here: Getting the path of a nuget package programmatically例如,这是将这些文件复制到packages/<package name>/Content/*.html中,不知何故我想我可以得到这个路径,比如这里给出的解决方案: Getting the path of a nuget ZEFE90A8E604A7C840D788D

But this looks complicated to me for such a seemingly simple task.但是对于这样一个看似简单的任务,这对我来说似乎很复杂。 What is the clean way of doing this?这样做的干净方法是什么?

Edit: Renamed question to specify that setting a resource is not an option in this case as some of the functions I'm consuming in the NuGet package are requesting a file path and not a resource.编辑:重命名问题以指定在这种情况下设置资源不是一个选项,因为我在 NuGet package 中使用的某些功能正在请求文件路径而不是资源。

If this is a nuget package created by you, under any circumstances at runtime a external file should't be used, someone can alter it, if is needed add it as embedded resource .如果这是您创建的 nuget package,则在运行时任何情况下都不应使用外部文件,如果需要,可以将其更改为嵌入式资源

Anyway, at runtime I would get the folder where the.dll stays like this:无论如何,在运行时我会得到.dll 保持这样的文件夹:

 var currentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
 var rootFolder = Assembly.GetExecutingAssembly().Location.Split(currentAssemblyName)[0];

Edit - should work for both .NET Framework or dotnet core编辑- 应该适用于 .NET 框架或 dotnet 核心

My mistake, to get the real path for the DLL you need to search for the Assembly, because ExecutingAssembly is from where the.exe was launched我的错误,要获得 DLL 的真实路径,您需要搜索程序集,因为ExecutingAssembly是从启动 .exe 的位置

 var assembly = AppDomain.CurrentDomain
                          .GetAssemblies()
                          .SingleOrDefault(assembly => assembly.GetName().Name == "my.awsome.nuget");
 var location = assembly.Location; 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM