【问题标题】:read nuget.config programmatically in c#在 C# 中以编程方式读取 nuget.config
【发布时间】:2018-04-12 06:17:47
【问题描述】:

是否可以使用 Nuget.visualStudio、nuget.core 或 nuget.clients dll 读取 nuget.config 文件和包源。我可以解析 xml,但 nuget.dlls 中是否有任何开箱即用的逻辑

【问题讨论】:

    标签: nuget nuget-package-restore


    【解决方案1】:

    是的!您需要使用https://www.nuget.org/packages/NuGet.Configuration/4.6.2 提供的NuGet.Configuration 包。

    那么就可以使用下面的代码了——

    using NuGet.Configuration;
    using System;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                // basic implementation of nuget.config in code
                var setting = Settings.LoadSpecificSettings(@"f:\root", "nuget.config");
    
                // get sources 
                var packageSourceProvider = new PackageSourceProvider(setting);
                var sources = packageSourceProvider.LoadPackageSources();
    
                foreach(var source in sources)
                {
                    Console.WriteLine($"{source.Name}: {source.SourceUri}");
                }
            }
        }
    }
    

    这将生成以下输出 -

    NuGet.org: https://api.nuget.org/v3/index.json
    

    使用的示例配置文件(假设在路径f:\root\nuget.config)-

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="NuGet.org" value="https://api.nuget.org/v3/index.json" />
      </packageSources>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2011-01-25
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 2019-03-25
      • 2012-04-16
      • 2012-02-07
      • 2010-09-28
      相关资源
      最近更新 更多