-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cs
More file actions
44 lines (35 loc) · 1.35 KB
/
Copy pathConfig.cs
File metadata and controls
44 lines (35 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Microsoft.Extensions.Configuration;
namespace vsctest
{
public static class Config
{
public static string Name { get; set; }
public static ClientConfig client;
public static SampleObj sampleObj;
static Config()
{
var config = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json").Build();
Name = config.GetValue<string>("Name");
var section = config.GetSection(nameof(ClientConfig));
client = section.Get<ClientConfig>();
var sect = config.GetSection("SampleObj");
sampleObj = sect.Get<SampleObj>();
Console.WriteLine("get config value:");
Console.WriteLine("By Name: " + config.GetValue<string>("Name"));
Console.WriteLine("By Sec:Name: " + config.GetSection("SampleObj:PropName").Value);
Console.WriteLine("By Sec.GetValue: " + config.GetSection("SampleObj").GetValue<string>("PropName"));
}
}
public class ClientConfig
{
public bool IsEnabled { get; set; }
public string APIUrl { get; set; }
public string Timeout { get; set; }
}
public class SampleObj
{
public string PropName { get; set; }
}
}