简体   繁体   English

如何使用没有库但仅包含文件的nuget包

[英]How to use a nuget package that has no library in it but only files to be used

I have installed a nuget package (say 'foo', version=1.0.4) in my C# project that only has some data files in it and no library, that's why it can't be referenced in the project. 我在C#项目中安装了一个nuget包(例如'foo',版本= 1.0.4),其中只有一些数据文件,没有库,这就是为什么它无法在项目中引用的原因。 The files are located at the nuget package's default location ( {proj loc}\\packages\\foo.1.0.4\\lib\\net45\\ ). 这些文件位于nuget软件包的默认位置( {proj loc}\\packages\\foo.1.0.4\\lib\\net45\\ )。 How can I properly access/read these files in C# code? 如何使用C#代码正确访问/读取这些文件?

I know that I can access them by specifying their physical path on the disk, eg 我知道我可以通过在磁盘上指定它们的物理路径来访问它们,例如

string filePath = $"{Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))}\\packages\\foo.1.0.4\\lib\net45\\bar.ttf";

But I don't want to do that as part of the path is hard-coded and that will change whenever a newer version of the 'foo' nuget package (ie this part '\\foo.1.0.4\\') is installed. 但是我不想这样做,因为路径的一部分是硬编码的,并且每当安装了新版本的'foo'nuget软件包(即此部分'\\ foo.1.0.4 \\)时,情况都会改变。

UPDATE: I've figured out a fully dynamic solution, Please see it at https://stackoverflow.com/a/54459367/1300390 更新:我已经找到了一个完全动态的解决方案,请在https://stackoverflow.com/a/54459367/1300390上查看

Since your code is depending on an external resource which is subject to changes, you should consider creating a directory in your project root. 由于代码取决于更改的外部资源,因此应考虑在项目根目录中创建目录。

MyProj - MyTiffStorage (New folder) MyProj-MyTiffStorage(新文件夹)

And change your code accordingly to point to this directory. 并相应地更改您的代码以指向该目录。 But in this case you need to be mindful to copy the files from the package (...lib\\net45) whenever there is an update to the package. 但是在这种情况下,只要软件包有更新,就需要注意从软件包(... lib \\ net45)复制文件。

This way for your code there is always a single source of truth and so the code need not change each time there is an update. 通过这种方式,您的代码始终只有一个真实来源,因此无需在每次更新时更改代码。 However as mentioned be mindful to copy the files manually. 但是,如前所述,请注意手动复制文件。

Thank You 谢谢

From your comment: 根据您的评论:

There can be a no. 可能没有。 of scenarios where using nuget package can prove to be quite efficient and smart for non lib files, for example in our case, those files are generated by another project and a no. 对于非lib文件,使用nuget包可以证明非常有效和聪明,例如,在我们的情况下,这些文件是由另一个项目生成的,而否。 of other client projects need to include/use those files if they need them along with availing the opportunity to get their updated versions as and when they are available, and nuget package management provides a well organized mechanism in this case. 其他客户端项目中,如果需要它们,则需要包含/使用这些文件,并有机会在可用时获取更新的版本,在这种情况下,nuget软件包管理提供了组织良好的机制。

One solution is to create a thin layer library with an interface to access those files (package it in the same nuget). 一种解决方案是创建一个带有访问这些文件的接口的薄层库(将其打包在同一nuget中)。

An alternate route to consider if you are using git is to use submodules instead of nuget packages. 考虑使用git的另一种方法是使用子模块而不是nuget包。 If this is viable depends on your specific workflow. 是否可行取决于您的特定工作流程。

After playing around with different approaches and attempts, finally I figured out the following one that quite nicely solves this problem: 在尝试了不同的方法和尝试之后,我终于找到了可以很好地解决此问题的以下方法:

The files from the nuget package's default download folder can be accessed/read without defining any varying hard-coded part as: 可以访问/读取nuget软件包默认下载文件夹中的文件,而无需将任何不同的硬编码部分定义为:

string appDir = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
string filePath = Directory.GetFiles(appDir, "baar.ttf", SearchOption.AllDirectories).First();

UPDATE: 更新:

To make it a bit more faster, the search space can be reduced further to the 'packages' folder only, as: 为了使其更快一点,可以将搜索空间进一步减少到“ packages”文件夹中,如下所示:

string nugetPackagesDir = $"{Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))}\\packages";
string filePath = Directory.GetFiles(nugetPackagesDir, "baar.ttf", SearchOption.AllDirectories).First();

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

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