关于堆栈有一个类似的帖子,但它可能对我的问题没有帮助,因为我使用的是Visual Studio 2015。
如何在VS2015中显示“启用NuGet包恢复”选项?
我选择文件>新项目,并创建一个空ASP。NET Web应用程序。我在找这个菜单项。
我应该提到,我已经在我的项目文件夹中寻找任何预先存在的nuGet文件,没有。
关于堆栈有一个类似的帖子,但它可能对我的问题没有帮助,因为我使用的是Visual Studio 2015。
如何在VS2015中显示“启用NuGet包恢复”选项?
我选择文件>新项目,并创建一个空ASP。NET Web应用程序。我在找这个菜单项。
我应该提到,我已经在我的项目文件夹中寻找任何预先存在的nuGet文件,没有。
当前回答
花了很长时间,但我最终找到了这个关于将msbuild集成解决方案迁移到自动包恢复的文档,我能够使用这里描述的方法解决这个问题。
去掉'。Nuget的解决方案目录从解决方案 删除对nuget的所有引用。目标从你的。csproj或。vbproj文件。虽然不受官方支持,但如果您有很多需要清理的项目,该文档会链接到一个PowerShell脚本。我手动编辑了我的,所以我不能就我的体验给出任何反馈。
当你手动编辑你的文件时,下面是你要找的:
方案文件(.sln)
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F4AEBB8B-A367-424E-8B14-F611C9667A85}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
项目文件(。Csproj / .vbproj)
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<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>
其他回答
当将带有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";
}
}
}
我想对于asp.net 4项目,我们将转向自动恢复,所以不需要这样做。对于较老的项目,我认为需要进行一些转换。
http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
对于.NET核心项目,在NuGet包管理器控制台中运行dotnet restore或dotnet build命令(会自动运行恢复)
您可以从运行控制台
> NuGet包管理器>包管理器控制台
在尝试构建示例项目gplus-quickstart-csharp-master时,我也遇到了同样的问题。
我仔细查看了错误信息,并从克服这个错误中找到了一个解决方案,希望这将有所帮助。
右键单击解决方案文件并在windows资源管理器中打开。 用NuGet复制. NuGet文件夹。Config, NuGet.exe, NuGet。目标(下载链接或简单地从其他项目复制并替换) 尝试重新构建解决方案。
享受! !
也可能是在试图安装软件包时运行该程序的结果。当内置的IIS在后台运行时,如果你试图点击它,它会变成灰色。