依赖于 Visual C++ 2013 运行时的 NuGet 包

NuGet package with a dependency on Visual C++ 2013 Runtime

我已经从 .NET4.0 DLL 创建了一个 NuGet 包,其中包含混合(托管和本机)代码。

本机代码打包在 .NET4.0 DLL 中,但依赖于 Visual C++ 2013 Redistributable

我正在想办法用 NuGet 包打包 redist and/or 警告用户他们需要它,但我一无所获。

有人有什么想法吗?

实际上我自己解决了这个问题。虽然我找不到将 VCpp 运行时作为 NuGet 包的依赖项包含在内的解决方案,但我确实找到了警告用户需要 Visual C++ 2013 运行时的解决方案。

我 运行 此代码一次,静态地,在需要 VC++ 运行时的 component/library 启动时:

    private static void AssertVcppDependencies()
    {
        var system32Path = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
        var system64Path = Environment.GetFolderPath(Environment.SpecialFolder.System);

        string platform = Environment.Is64BitProcess ? "x64 and x86" : "x86";
        bool success = File.Exists(Path.Combine(system32Path, MSVCP120DllName));

        if (Environment.Is64BitProcess)
        {
            success &= File.Exists(Path.Combine(system64Path, MSVCP120DllName));
        }            

        if (!success)
        {
            throw new InvalidOperationException("This Application Requires the Visual C++ 2013 " + platform + 
                " Runtime to be installed on this computer. Please download and install it from https://www.microsoft.com/en-GB/download/details.aspx?id=40784");
        }
    }

这应该提醒所有正在使用您的 NuGet 包的开发人员他们需要在 运行 时间安装。