0

After a user does a dotnet add package <SomePackage>, the DLL will be installed to a path similar to:

C:\Users\USER\.nuget\packages\SomePackage\1.0.2\lib\netstandard2.0\SomePackage.dll

How can I find this path programmatically? I see there's some information in the obj/project.assets.json that I could parse, and also an interesting DependencyContextJsonReader class under the dotnet github organization.

However, there's no documentation or discussions about this that I can find, and I'm not sure if that's because I'm taking the wrong approach.

Some background: I'm writing a tool that can scaffold C# projects. It uses a combination of the dotnet command line tools, and the Roslyn Workspace APIs to interact with the generated project. I'd now like to allow the user to install Nuget packages into this generated solution. I invoke dotnet add package SomePackage which works fine, and now I'd like to add the DLL to the Roslyn Project object using the AddReferences API, which requires the actual DLL.

Will
  • 2,086
  • 23
  • 30
  • Since this is an XY problem, can you explain more in detail what your previous problem is? When you use the `nuget` command, it will install the dlls in the correct `packages/` directory in your solution file, you don't need to go into the `.nuget` directory of the user. Edit your question to add the new information. – Progman Mar 03 '19 at 09:51
  • @Progman thanks. The solution's `packages/` directory is deprecated in favor of the user's home directory, due to the recent PackageReference migration. I've added some background information about the specific operation I'm doing with Roslyn compiler API. – Will Mar 03 '19 at 15:17
  • Roslyn/MSBuild should have supported package reference for a while, so I wonder why you still try to add a file based reference. Most of the documentation comes from existing discussions on GitHub, https://github.com/dotnet/roslyn/issues so you might try it too. – Lex Li Mar 03 '19 at 17:16
  • Thanks @LexLi, I'm using the Roslyn Workspace API which only supports adding references by string filepaths, I think (https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.metadatareference?view=roslyn-dotnet) – Will Mar 04 '19 at 05:51

1 Answers1

1

I found a way to do this using the nuget client libraries.

Essentially, rather than shelling out to the dotnet add package command, I can use the NuGet client libraries directly from my application to install packages. I then have access to the full path via the PackagePathResolver.GetInstalledPath method.

Martin Björkström's post, Revisiting the NuGet v3 Libraries, goes into much more detail, and a fully working code sample from Martin is available in this gist.

Mufaka
  • 2,333
  • 1
  • 18
  • 25
Will
  • 2,086
  • 23
  • 30