如何从提供者(c#.NET Core 控制台应用程序)获取参数?
How to get Args from Provider (c# .NET Core console app)?
我尝试用单例制作一个 .NET Core 控制台应用程序。我主要有:
Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddCommandLine(args)
.Build();
var services = ConfigureServices();
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetService<App>().Run();
在App.cs中:
public App(IConfiguration config, IConfigurationRoot configuration)
{
conf = config;
var list = configuration.Providers.ToList();
var provider = (CommandLineConfigurationProvider)list[1];
}
如何使用提供商的参数下载 string[]
(或列表,或其他)?
答案由Martin Costello提供(来自评论):
You can use Environment.GetCommanLineArgs()
to get the raw command line to the application. The providers are an abstraction, so don't give you access to exactly what was passed into them.
Link: docs.microsoft.com: GetCommandLineArgs
我尝试用单例制作一个 .NET Core 控制台应用程序。我主要有:
Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddCommandLine(args)
.Build();
var services = ConfigureServices();
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetService<App>().Run();
在App.cs中:
public App(IConfiguration config, IConfigurationRoot configuration)
{
conf = config;
var list = configuration.Providers.ToList();
var provider = (CommandLineConfigurationProvider)list[1];
}
如何使用提供商的参数下载 string[]
(或列表,或其他)?
答案由Martin Costello提供(来自评论):
You can use
Environment.GetCommanLineArgs()
to get the raw command line to the application. The providers are an abstraction, so don't give you access to exactly what was passed into them.
Link: docs.microsoft.com: GetCommandLineArgs