关于堆栈有一个类似的帖子,但它可能对我的问题没有帮助,因为我使用的是Visual Studio 2015。

如何在VS2015中显示“启用NuGet包恢复”选项?

我选择文件>新项目,并创建一个空ASP。NET Web应用程序。我在找这个菜单项。

我应该提到,我已经在我的项目文件夹中寻找任何预先存在的nuGet文件,没有。


当前回答

我使用msbuild /t:restore。


资料来源:

我的问题是用MSBuild 所以我遵循了@Vinney Kelly的链接:将msbuild集成解决方案迁移到自动包恢复

和…

这招很管用=]

MSBuild:使用MSBuild /t:restore命令恢复包 项目文件中列出的包(仅限PackageReference)。可用 只有在NuGet 4。和MSBuild 15.1+,它们包含在Visual . x+中 2017工作室。Nuget restore和dotnet restore都使用该命令 适用项目。

其他回答

Ivan Branets的解决方案基本上为我解决了这个问题,但更多的细节可以分享。

在我的情况下,我在VS 2015使用自动包恢复和TFS。这些都是默认的东西。

问题是,当另一个开发人员试图从TFS获得解决方案时,一些包没有完全恢复。(为什么,我还不太确定。)但是packages文件夹包含了一个用于引用和NuGet包的文件夹,但是它没有被展开(比如一个包含.dll的lib文件夹丢失了)。这一半,但不是很正确的概念阻止了包装的恢复。

您可以认出这一点,因为该引用将有一个黄色感叹号,表示未被解析。

因此,删除包中的文件夹的解决方案消除了包恢复阻塞问题。然后,您可以在顶部解决方案级别上单击右键,以获得恢复包的选项,现在它应该可以工作了。

微软已经在VS2015中放弃了对“启用NuGet包恢复”的支持,你需要做一些手动更改来迁移旧的解决方案或将该功能添加到新的解决方案中。这个新特性在NuGet包还原中有很好的描述。

这里还有一个针对现有项目的迁移指南(如前所述):NuGet迁移指南

当升级:

不要删除。nuget目录。 删除nuget.exe和nuget.exe。目标文件。 保留nuget.config。 手动清除每个项目文件中对NuGet目标的任何引用。上面提到的Powershell脚本似乎弊大于利。

创建新项目时:

In your Visual Studio 2015 solution, create a Solution Directory called .nuget. Create an actual directory of the solution directory (where the .sln file lives) and call it .nuget (note that the solution directory is not the same as the actual file system directory even though they have the same name). Create a file in the .nuget directory called nuget.config. Add the 'nuget.config' to the solution directory created in step #2. Place the following text in the nuget.config file: <?xml version="1.0" encoding="utf-8"?> <configuration> <config> <add key="repositorypath" value="$\..\..\..\..\Packages" /> </config> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> </configuration>

这个配置文件将允许您将所有包合并到一个地方,这样您就不会在文件系统中有相同包的20个不同副本。相对路径将根据您的解决方案目录体系结构而改变,但它应该指向所有解决方案的公共目录。

在执行步骤5后,需要重新启动visual studio。除非你这样做,否则Nuget不会发现这些变化。

最后,您可能必须使用“Nuget包管理器的解决方案”卸载,然后重新安装包。我不知道这是否是我运行的Powershell脚本的副作用,或者只是踢NuGet回到齿轮的方法。当我完成了所有这些步骤后,当我从TFVC签出项目时,我复杂的构建体系结构完美地运行了新的包。

您可以选择从“packages”文件夹中删除所有文件夹,并选择“Manage NuGet packages for Solution…”。在这种情况下,“恢复”按钮出现在NuGet Packages Windows上。

如果其他人在Visual Studio 2017中发现这个问题,请确保项目是通过.sln文件而不是文件夹打开的,因为Visual Studio如果按文件夹打开则不会拾取设置。如果您正在为git使用Visual Studio在线服务,则默认会发生这种情况。

当将带有nuget包的项目从Vx20XX升级到VS2015时,您可能会遇到nuget包的问题。

错误信息示例: 该项目引用了这台计算机上缺少的NuGet包。启用NuGet包还原来下载它们。

更新2016-02-06:我有一个链接的信息,但它不再工作了。我怀疑最近的路径已经解决了这个问题??

我解决了我的问题,写了一个小程序,做msbuild集成包恢复vs.自动包恢复

您可以在这里下载该工具的可执行文件。

请让我知道结果!

代码参考:

<Window x:Class="FixNuGetProblemsInVs2015.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FixNuGetProblemsInVs2015"
        mc:Ignorable="d"
        Title="Fix NuGet Packages problems in Visual Studio 2015 (By Eric Ouellet)" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="10"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0">Root directory of projects</TextBlock>
        <Grid Grid.Row="0" Grid.Column="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <TextBox Grid.Column="0" Name="DirProjects"></TextBox>
            <Button Grid.Column="1" VerticalAlignment="Bottom" Name="BrowseDirProjects" Click="BrowseDirProjectsOnClick">Browse...</Button>
        </Grid>

        <!--<TextBlock Grid.Row="1" Grid.Column="0">Directory of NuGet Packages</TextBlock>
        <Grid Grid.Row="1" Grid.Column="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <TextBox Grid.Column="0" Name="DirPackages"></TextBox>
            <Button Grid.Column="1"  Name="BrowseDirPackages" Click="BrowseDirPackagesOnClick">Browse...</Button>
        </Grid>-->

        <TextBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Name="TxtLog" IsReadOnly="True"></TextBox>

        <Button Grid.Row="3" Grid.Column="0" Click="ButtonRevertOnClick">Revert back</Button>
        <Button Grid.Row="3" Grid.Column="2" Click="ButtonFixOnClick">Fix</Button>
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;

/// <summary>
/// Applying recommanded modifications in section : "MSBuild-Integrated package restore vs. Automatic Package Restore"
/// of : http://docs.nuget.org/Consume/Package-Restore/Migrating-to-Automatic-Package-Restore
/// </summary>

namespace FixNuGetProblemsInVs2015
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DirProjects.Text = @"c:\prj";
            // DirPackages.Text = @"C:\PRJ\NuGetPackages";
        }

        private void BrowseDirProjectsOnClick(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.SelectedPath = DirProjects.Text;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DirProjects.Text = dlg.SelectedPath;
            }
        }

        //private void BrowseDirPackagesOnClick(object sender, RoutedEventArgs e)
        //{
        //  FolderBrowserDialog dlg = new FolderBrowserDialog();
        //  dlg.SelectedPath = DirPackages.Text;
        //  if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //  {
        //      DirPackages.Text = dlg.SelectedPath;
        //  }
        //}

        // private string _dirPackages;

        private void ButtonFixOnClick(object sender, RoutedEventArgs e)
        {
            DoJob(false);
        }

        private void ButtonRevertOnClick(object sender, RoutedEventArgs e)
        {
            DoJob(true);
        }

        private void DoJob(bool revert = false)
        {
            TxtLog.Text = "";

            string dirProjects = DirProjects.Text;
            // _dirPackages = DirPackages.Text;

            if (!Directory.Exists(dirProjects))
            {
                MessageBox.Show("Projects directory does not exists: " + dirProjects);
                return;
            }

            //if (!Directory.Exists(_dirPackages))
            //{
            //  MessageBox.Show("Packages directory does not exists: " + _dirPackages);
            //  return;
            //}

            RecurseFolder(dirProjects, revert);
        }

        private void RecurseFolder(string dirProjects, bool revert = false)
        {
            if (revert)
            {
                Revert(dirProjects);
            }
            else
            {
                FixFolder(dirProjects);
            }

            foreach (string subfolder in Directory.EnumerateDirectories(dirProjects))
            {
                RecurseFolder(subfolder, revert);
            }
        }

        private const string BackupSuffix = ".fix_nuget_backup";

        private void Revert(string dirProject)
        {
            foreach (string filename in Directory.EnumerateFiles(dirProject))
            {
                if (filename.ToLower().EndsWith(BackupSuffix))
                {
                    string original = filename.Substring(0, filename.Length - BackupSuffix.Length);
                    if (File.Exists(original))
                    {
                        File.Delete(original);                                          
                    }
                    File.Move(filename, original);
                    Log("File reverted: " + filename + " ==> " + original);
                }
            }
        }

        private void FixFolder(string dirProject)
        {
            BackupFile(System.IO.Path.Combine(dirProject, "nuget.targets"));
            BackupFile(System.IO.Path.Combine(dirProject, "nuget.exe"));

            foreach (string filename in Directory.EnumerateFiles(dirProject))
            {
                if (filename.ToLower().EndsWith(".csproj"))
                {
                    FromProjectFileRemoveNugetTargets(filename);
                }
            }
        }

        private void BackupFile(string path)
        {
            if (File.Exists(path))
            {
                string backup = path + BackupSuffix;
                if (!File.Exists(backup))
                {
                    File.Move(path, backup);
                    Log("File backup: " + backup);
                }
                else
                {
                    Log("Project has already a backup: " + backup);
                }
            }
        }

        private void FromProjectFileRemoveNugetTargets(string prjFilename)
        {
            XDocument xml = XDocument.Load(prjFilename);

            List<XElement> elementsToRemove = new List<XElement>();

            foreach (XElement element in xml.Descendants())
            {
                if (element.Name.LocalName == "Import")
                {
                    var att = element.Attribute("Project");
                    if (att != null)
                    {
                        if (att.Value.Contains("NuGet.targets"))
                        {
                            elementsToRemove.Add(element);
                        }
                    }
                }

                if (element.Name.LocalName == "Target")
                {
                    var att = element.Attribute("Name");
                    if (att != null && att.Value == "EnsureNuGetPackageBuildImports")
                    {
                        elementsToRemove.Add(element);
                    }
                }
            }

            if (elementsToRemove.Count > 0)
            {
                elementsToRemove.ForEach(element => element.Remove());
                BackupFile(prjFilename);
                xml.Save(prjFilename);
                Log("Project updated: " + prjFilename);
            }
        }

        private void Log(string msg)
        {
            TxtLog.Text += msg + "\r\n";
        }

    }
}