C# 无法卸载/重新安装 NuGet 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18630332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:48:07  来源:igfitidea点击:

Can't uninstall/reinstall NuGet package

c#visual-studiowindows-phone-8nugetpackages

提问by user826955

I've set up my project with Visual Studio Express 2012, added some C# code, and successfully compiled/deployed to emulator. At some point I decided I want to do something with JSON, and I found that I should use the Json.NET framework, which is available as a NuGet package. I have added this framework successfully using the command Install-Package Newtonsoft.Json.

我已经使用 Visual Studio Express 2012 设置了我的项目,添加了一些 C# 代码,并成功编译/部署到模拟器。在某个时候,我决定用 JSON 做点什么,我发现我应该使用 Json.NET 框架,它可以作为 NuGet 包使用。我已经使用命令成功添加了这个框架Install-Package Newtonsoft.Json

I tried to install an update which MSVC offered two days ago, and the installation failed, leaving me unable to open my project again. Reinstalling MSVC didn't help, so I ended up restoring Windows to a previous state. I was able to open my project again, and I wanted to reinstall the NuGet package (not knowing it is installed inside the project, notMSVC). It gave me errors upon installing, so I tried to uninstall everything from package console.

我尝试安装 MSVC 两天前提供的更新,但安装失败,使我无法再次打开我的项目。重新安装 MSVC 没有帮助,所以我最终将 Windows 恢复到以前的状态。我能够再次打开我的项目,我想重新安装 NuGet 包(不知道它安装在项目内部,而不是MSVC)。它在安装时给了我错误,所以我尝试从包控制台卸载所有内容。

Long story short:

长话短说:

  • Nuget package console says "no packages installed" when Get-Package
  • Running Install-Package Newtonsoft.Jsonagain gives me the error

    Newtonsoft.Json 5.0.6 could not be installed. You try to install this package in a project referencing "WindowsPhone,Version=v8.0", the package however does not contain assemblies compatible with this framework

  • The "Manage NuGet" packages dialog shows no packagesunder "installed", however in the online package, the json package is listed and marked as installed (so I cannot try to install it)
  • Nuget 软件包控制台显示“未安装软件包”时 Get-Package
  • Install-Package Newtonsoft.Json再次运行给了我错误

    无法安装 Newtonsoft.Json 5.0.6。您尝试在引用“WindowsPhone,Version=v8.0”的项目中安装此包,但是该包不包含与此框架兼容的程序集

  • “管理 NuGet”包对话框在“已安装”下没有显示,但是在在线包中,json 包被列出并标记为已安装(所以我无法尝试安装它)

Now I'm completely lost. I don't know how to install the package properly, nor how to remove it properly. Is my project file broken? How can I repair it?

现在我完全迷失了。我不知道如何正确安装软件包,也不知道如何正确删除它。我的项目文件坏了吗?我该如何修复它?



[edit]Now it gets even more confusing. I've just updated the NuGet package manager (not MSVC though), and tried to reinstall Json.NET.

[编辑]现在它变得更加混乱。我刚刚更新了 NuGet 包管理器(虽然不是 MSVC),并尝试重新安装 Json.NET。

  • In the dialog it's still not shown under "installed"
  • In the online packages list it is no more marked as installed, but clicking "Install" just does nothing
  • Get-Packagein the console shows the package as installed

    PM> Get-Package
    
    Id                             Version              Description/Release Notes                                                                                                                                                                                            
    --                             -------              -------------------------                                                                                                                                                                                            
    Newtonsoft.Json                5.0.6                Json.NET is a popular high-performance JSON framework for .NET
    
  • Trying to uninstall with `Uninstall-Package Newtonsoft.Json" gives "The Package 'Newtonsoft.Json' could not be found"

  • In my project folder I can see a "Newtonsoft.Json.5.0.6" subfolder in the packagesdirectory
  • 在对话框中,它仍然没有显示在“已安装”下
  • 在在线软件包列表中,它不再被标记为已安装,但单击“安装”什么也不做
  • Get-Package在控制台中显示已安装的包

    PM> Get-Package
    
    Id                             Version              Description/Release Notes                                                                                                                                                                                            
    --                             -------              -------------------------                                                                                                                                                                                            
    Newtonsoft.Json                5.0.6                Json.NET is a popular high-performance JSON framework for .NET
    
  • 尝试使用“Uninstall-Package Newtonsoft.Json”卸载会出现“找不到包‘Newtonsoft.Json’”

  • 在我的项目文件夹中,我可以在packages目录中看到一个“Newtonsoft.Json.5.0.6”子文件夹

Should I just start over from scratch and create a new project? :/

我应该从头开始并创建一个新项目吗?:/



Contents of packages.dgml:

package.dgml 的内容:

<?xml version="1.0" encoding="utf-8"?>
<DirectedGraph GraphDirection="LeftToRight" xmlns="http://schemas.microsoft.com/vs/2009/dgml">
  <Nodes />
  <Links />
  <Categories>
    <Category Id="Projekt" />
    <Category Id="Paket" />
  </Categories>
  <Styles>
    <Style TargetType="Node" GroupLabel="Projekt" ValueLabel="True">
      <Condition Expression="HasCategory('Projekt')" />
      <Setter Property="Background" Value="Blue" />
    </Style>
  </Styles>
</DirectedGraph>

采纳答案by paqogomez

In your Solution or Project you will find a file called packages.config. Open this file and you will see all the packages that NuGethas installed.

在您的解决方案或项目中,您会找到一个名为packages.config. 打开这个文件,你会看到所有NuGet已经安装的包。

The file will look something like this:

该文件将如下所示:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Json" version="2.0.3" targetFramework="net45" />
</packages>

Simply delete the line of your package and save the file.

只需删除包的行并保存文件。

<?xml version="1.0" encoding="utf-8"?>
<packages>
</packages>

Then run NuGetagain and it should install.

然后NuGet再次运行,它应该安装。

The JSON.NET NuGet package should just work with a Windows Phone 8.0 project. Adding it in VS2012 NuGet manager pulls in the WP7 version of JSON.NET. (packages\Newtonsoft.Json.4.5.10\lib\sl3-wp\Newtonsoft.Json.dll)

JSON.NET NuGet 包应该只适用于 Windows Phone 8.0 项目。在 VS2012 NuGet 管理器中添加它会引入 JSON.NET 的 WP7 版本。(包\Newtonsoft.Json.4.5.10\lib\sl3-wp\Newtonsoft.Json.dll)

JSON.NET is now also available as a Portable Class Library which you can consume from WP8 (available in NuGet or in source form).

JSON.NET 现在也可用作可移植类库,您可以从 WP8 使用它(在 NuGet 或源形式中可用)。

UPDATE:

更新:

The latest version of NuGet has an uninstall.

最新版本的 NuGet 具有卸载功能。

VS 2013:

与 2013 年相比:

在解决方案中,右键单击“引用”和“管理 NuGet 包”,您会在左上角找到“已安装的包”。选择软件包,然后将提供卸载选项。

VS 2015:

与 2015 年相比:

在解决方案中,右键单击“引用”和“管理 NuGet 包”,找到右上角的“已安装”选项卡。将鼠标悬停在软件包上,“X”将出现以卸载。

VS 2017:

VS 2017:

在解决方案上,右键单击并“管理解决方案的 NuGet 包”,找到左上角的“已安装”选项卡。单击软件包,右侧面板中将出现“卸载”按钮。

VS 2019:

VS 2019:

(与2017相同)在解决方案上,右键单击“管理解决方案的NuGet包”,找到左上角的“已安装”选项卡。单击软件包,右侧面板中将出现“卸载”按钮。

回答by Alex Paven

It is indeed a bit confusing, but there are different options available if you right-click the solution and choose manage nuget packages. You should be able to uninstall from there.

这确实有点令人困惑,但是如果您右键单击解决方案并选择管理 nuget 包,则可以使用不同的选项。您应该可以从那里卸载。

You should check the answer here: Windows Phone 8 JSONfor more information on Json.net and WP8.

您应该在此处查看答案:Windows Phone 8 JSON以获取有关 Json.net 和 WP8 的更多信息。

回答by Guish

I had the same problem. I wasn't able to re-install my package even after all the package.config modifications. Here what work for me.

我有同样的问题。即使在所有 package.config 修改之后,我也无法重新安装我的包。这里有什么对我有用。

In your solution folder there should be a "packages" directory. Open this directory and delete the directory associated with your package.

在您的解决方案文件夹中应该有一个“包”目录。打开此目录并删除与您的包关联的目录。

Done.

完毕。

回答by Ogglas

Just sat four hours on my new Windows 10 machine and couldn't figure out why no references worked in Visual Studio 2015 and why I could not restore NuGet packages. For some reason TFS added two packages folders:

刚刚在我的新 Windows 10 机器上坐了四个小时,无法弄清楚为什么没有引用在 Visual Studio 2015 中工作以及为什么我无法恢复 NuGet 包。出于某种原因,TFS 添加了两个包文件夹:

Locations:

地点:

C:\Users\YourUser\Documents\Visual Studio 2015\Projects\YourProject\packages

C:\Users\YourUser\.nuget\packages

C:\Users\YourUser\Documents\Visual Studio 2015\Projects\YourProject\packages

C:\Users\YourUser\.nuget\packages

When both of the package folders were removed I could restore NuGet packages and everything worked again.

当两个包文件夹都被删除后,我可以恢复 NuGet 包,一切都恢复正常了。

回答by Mina Fawzy

I tried this solution and its works

我试过这个解决方案和它的作品

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

回答by pensono

I had a similar problem in VS2017 (fresh install). I was working with an older project trying to install a relatively new package. When I would install the package from the GUI, it flashed for a second, then did nothing.

我在 VS2017(全新安装)中遇到了类似的问题。我正在处理一个较旧的项目,试图安装一个相对较新的软件包。当我从 GUI 安装软件包时,它闪烁了一秒钟,然后什么也没做。

The problem was that my project had a lower .NET target than the package. (My solution was targeting 4.5, and the package targeted 4.6). Changing the .NET target to a new enough version in my project configuration and rebuilding fixed the issue.

问题是我的项目的 .NET 目标低于包。(我的解决方案是针对 4.5,而软件包针对 4.6)。在我的项目配置中将 .NET 目标更改为足够新的版本并重建解决了该问题。

This issue can be seen by running the following commands: (Names changed)

通过运行以下命令可以看到此问题:(名称已更改)

PM> Install-Package Package.NET

Attempting to gather dependency information for package 'Package.NET.1.0.0' with respect to project 'MyProject', targeting '.NETFramework,Version=v4.5'
Gathering dependency information took 0.19 ms
Attempting to resolve dependencies for package 'Package.NET.1.0.0' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package 'Package.NET.1.0.0'
Resolved actions to install package 'Package.NET.1.0.0'
Retrieving package 'Package.NET.1.0.0' from 'nuget.org'.
Install failed. Rolling back...
Package 'Package.NET.1.0.0' does not exist in project 'MyProject'
Package 'Package.NET.1.0.0' does not exist in folder 'C:\Code\MyProject\packages'
Executing nuget actions took 13.92 ms
Install-Package : Could not install package 'Package.NET.1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', 
but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
At line:1 char:1
+ Install-Package Package.NET
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand