ConfigHelper.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Microsoft.Extensions.Configuration;
  2. using System;
  3. using System.IO;
  4. namespace wispro.sp.utility
  5. {
  6. public class ConfigHelper
  7. {
  8. private static IConfiguration _configuration;
  9. static ConfigHelper()
  10. {
  11. //在当前目录或者根目录中寻找appsettings.json文件
  12. var fileName = "appsettings.json";
  13. var directory = AppContext.BaseDirectory;
  14. directory = directory.Replace("\\", "/");
  15. var filePath = $"{directory}/{fileName}";
  16. if (!File.Exists(filePath))
  17. {
  18. var length = directory.IndexOf("/bin");
  19. filePath = $"{directory.Substring(0, length)}/{fileName}";
  20. }
  21. var builder = new ConfigurationBuilder()
  22. .AddJsonFile(filePath, false, true);
  23. _configuration = builder.Build();
  24. }
  25. //获取值
  26. public static string GetSectionValue(string key)
  27. {
  28. return _configuration.GetSection(key).Value;
  29. }
  30. }
  31. }