How do I install a NuGet package .nupkg file local

2019-01-03 00:49发布

I have some .nupkg files from a C# book. How can I install them?

Can't see my packages

6条回答
放荡不羁爱自由
2楼-- · 2019-01-03 01:08

For Visual Studio 2017 and its new .csproj format

You can no longer just use Install-Package to point to a local file. (That's likely because the PackageReference element doesn't support file paths; it only allows you to specify the package's Id.)

You first have to tell Visual Studio about the location of your package, and then you can add it to a project. What most people do is go into the NuGet Package Manager and add the local folder as a source (menu ToolsOptionsNuGet Package ManagerPackage Sources). But that means your dependency's location isn't committed (to version-control) with the rest of your codebase.

Local NuGet packages using a relative path

This will add a package source that only applies to a specific solution, and you can use relative paths.

You need to create a nuget.config file in the same directory as your .sln file. Configure the file with the package source(s) you want. When you next open the solution in Visual Studio 2017, any .nupkg files from those source folders will be available. (You'll see the source(s) listed in the Package Manager, and you'll find the packages on the "Browse" tab when you're managing packages for a project.)

Here's an example nuget.config to get you started:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <add key="MyLocalSharedSource" value="..\..\..\some\folder" />
    </packageSources>
</configuration>

Backstory

My use case for this functionality is that I have multiple instances of a single code repository on my machine. There's a shared library within the codebase that's published/deployed as a .nupkg file. This approach allows the various dependent solutions throughout our codebase to use the package within the same repository instance. Also, someone with a fresh install of Visual Studio 2017 can just checkout the code wherever they want, and the dependent solutions will successfully restore and build.

查看更多
【Aperson】
3楼-- · 2019-01-03 01:13

You can also use the Package Manager Console and invoke the Install-Package cmdlet by specifying the path to the directory that contains the package file in the -Source parameter:

Install-Package SomePackage -Source C:\PathToThePackageDir\
查看更多
Viruses.
4楼-- · 2019-01-03 01:15

Menu ToolsOptionsPackage Manager

Enter image description here

Give a name and folder location. Click OK. Drop your NuGet package files in that folder.

Go to your Project, right click and select "Manage NuGet Packages" and select your new package source.

Enter image description here

Here is the documentation.

查看更多
可以哭但决不认输i
5楼-- · 2019-01-03 01:16
  1. Add the files to a folder called LocalPackages next to you solution (it doesn't have to be called that, but adjust the xml in the following step accordingly)
  2. Create a file called NuGet.config next to your solution file with the following contents

    <?xml version="1.0" encoding="utf-8"?>
      <configuration>
        <packageSources>
          <add key="LocalPackages" value="./LocalPackages" />
        </packageSources>
        <activePackageSource>
          <!-- this tells that all of them are active -->
          <add key="All" value="(Aggregate source)" />
        </activePackageSource>
     </configuration>
    
  3. If the solution is open in Visual Studio, close it, then re-open it.

Now your packages should appear in the browser, or be installable using Install-Package

查看更多
ら.Afraid
6楼-- · 2019-01-03 01:17

If you have a .nupkg file and just need the .dll file all you have to do is change the extension to .zip and find the lib directory.

查看更多
Root(大扎)
7楼-- · 2019-01-03 01:19

For .nupkg files I like to use:

Install-Package C:\Path\To\Some\File.nupkg
查看更多
登录 后发表回答