ドライ:.NETカスタム設定ファイルを達成読んコア、ソースコード、ああ

楽観人は、.NET COREで設定ファイルを読み込む方法がわからない、私はappsettings.jsonで設定ファイルを読み込む方法についての入門情報に、ここでは2つの点を持っています。これは、あなたのカスタム設定ファイルをお教えします。

1.プロジェクト内のコンフィギュレーション・ファイルを作成します。

{
   "FILEMAP" :{
     "ImgPath": "D:\\ MYFILE \\雑貨\\ NPSPower \\ TemplateCore \\ TemplateCore \\ wwwrootの\\ UpImg \\"  "ImgWeb":「http://127.0.0.1 :1994 / UpImg / " "ファイルパス": "D:\\ myfileを\\ miscの\\ \\ NPSPower TemplateCore \\ TemplateCore \\ wwwrootに\\ UpFile \\"  "FileWeb":"のhttp://127.0。 0.1:1994 / UpFile / " "VideoPath": "D:\\ MYFILE \\雑貨\\ NPSPower \\ TemplateCore \\ TemplateCore \\ wwwrootの\\ UpVideo \\"  "VideoWeb":" HTTP://127.0 .0.1:1994 / UpVideo /」 "ウェブ": "http://127.0.0.1:1994/" 
  } 
}

 

2.参照ライブラリのMicrosoft.Extensions.Configuration.Json操作とプロファイルクラスConfigHelper.csを作成

インストール・パッケージMicrosoft.Extensions.Configuration.Json -Version 3.0.0

使用Microsoft.Extensions.Configuration;
 使用してシステム;
 使用System.Collections.Generic;
 使用してSystem.IO;
 使用System.Textの、

名前空間の共通
{ 
  パブリック  クラスConfigHelperに
    { 
        プライベート 静的IConfigurationの_configuration、

        静的ConfigHelperに()
        { 
            // カレントディレクトリをか探しルートディレクトリのファイル
            のvar fileNameに= " コンフィグ/ ManagerConfig.json " ; 

            のvarディレクトリ= AppContext.BaseDirectory; 
            ディレクトリ = directory.Replace(" \\ "" / " ); 

            VAR filePathに= $ " {ディレクトリ} / {fileNameに} " もし(!File.Exists(filePathに))
            { 
                VARの長さ= directory.IndexOf(" / binに" ); 
                filePathに = $ " {directory.Substring(0、長さ)} / {fileNameに} " 
            } 

            のvarビルダー= 新しいConfigurationBuilder()
                .AddJsonFile(filePathに、)。

            _configuration = builder.Build()。
        } 

        パブリック 静的 文字列 GetSectionValue(文字列キー)
        { 
            戻り_configuration.GetSectionを(キー).Valueの。
        } 
    } 
}

3.プロジェクトのコンフィギュレーションファイルを読みます

文字列 ImgPath = ConfigHelper.GetSectionValue(" FILEMAP:ImgPath " );
返す ImgPathを。

 

 

 

オープンソース住所:https://github.com/jiyuwu/TemplateCore

ブラウジング効果をテスト:http://127.0.0.1:1994/home/TestConfig

 

 

  あなたを助けるために、そして1をお勧めしてください、ありがとうございました。

おすすめ

転載: www.cnblogs.com/jiyuwu/p/11776298.html