从Main读取appsetting

using System;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Binder;
using Microsoft.Extensions.Configuration.EnvironmentVariables;
using Microsoft.Extensions.Configuration.FileExtensions;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration.UserSecrets;
using System.IO;

namespace OMSECFaPiaoSMS
{

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var builder = new ConfigurationBuilder()                   
                   .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                   .AddEnvironmentVariables();

                IConfigurationRoot configuration = builder.Build();
                B2BDB.ConnectionString = configuration.GetConnectionString("ss_xx");
                var settings = configuration.GetSection("AppSettings");
                string key = settings.GetValue<string>("key");
                string account = settings.GetValue<string>("account");
                string sdate = "";
                string whse = args[0];               
                if (args.Length > 1)
                {
                    DateTime tempdate;
                    if (DateTime.TryParse(args[1], out tempdate))
                    {
                        sdate = String.Format("{0:yyyy-MM-dd}", tempdate);
                    }
                    else
                        throw new Exception("日期格式错误!");
                }
                else
                {
                    sdate = String.Format("{0:yyyy-MM-dd}", System.DateTime.Now.AddDays(-3));
                }  
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
appsetting.json 文件

{
"ConnectionStrings": { "ss_xx": "Data Source=aaaaa;Initial Catalog=aaa;Persist Security Info=True;User ID=aa;Password=aaa;Connect Timeout=30" }, "AppSettings": { "account": "aaaa", "key": "aaaaa" }, "Logging": { "LogLevel": { "Default": "Warning" } } }

 

从Main读取appsetting

上一篇:Android利用碎片fragment实现底部标题栏(Github模板开源)


下一篇:Android如何屏蔽home键和recent键