app.config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--上次成功运行的时间,yyyy-MM-dd hh:mm:ss, 2021-10-25 01:01:01-->
    <add key="LastRunTime" value="2021-10-25 01:01:01" />
    <!--日志文件保存地址,D:\\Log\\-->
    <add key="LogFile" value="D:\\Log\\" />
  </appSettings>
</configuration>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 方法一(读取exe的共同app.config文件,没试过能不能读取本程序集的):

string dllAdress = logAdress =  ConfigurationSettings.AppSettings["keyname"].Trim();
  • 1.

 方法二(读取本程序集的app.config):

1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Reflection;
 7 using System.IO;
 8 using System.Configuration;
 9 
10 namespace SendDataZCCA.Common
11 {
12     // 程序集config文件的读取与修改
13     public class AppConfigTools
14     {
15         /// <summary>
16         /// 读取本程序集配置config的方法2
17         /// 方法一为:string dllAdress = logAdress =  ConfigurationSettings.AppSettings["LogFile"].Trim();
18         /// </summary>
19         /// <returns></returns>
20         public static Configuration GetConfig()
21         {
22             Assembly assembly = Assembly.GetCallingAssembly();
23             string path = string.Format("{0}.config", assembly.Location);
24             if (!File.Exists(path))
25             {
26                 throw new FileNotFoundException(path + "路径下的文件未找到!");
27             }
28             try
29             {
30                 ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
31                 configFile.ExeConfigFilename = path;
32                 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
33                 return config;
34             }
35             catch (Exception)
36             {
37                 throw;
38             }
39         }
40 
41         /// <summary>
42         /// 扩展1-configuration.appSettings.app格式转Dictionary格式
43         /// </summary>
44         /// <returns></returns>
45         public static Dictionary<string, string> GetConfigString()
46         {
47             try
48             {
49                 Dictionary<string, string> dictionary = new Dictionary<string, string>();
50                 Configuration config = GetConfig();
51                 var settings = config.AppSettings.Settings;
52                 foreach (var key in settings.AllKeys)
53                 {
54                     string value = settings[key].Value.ToString();
55                     dictionary.Add(key.ToString(), value);
56                 }
57                 return dictionary;
58             }
59             catch (Exception)
60             {
61                 throw;
62             }
63         }
64 
65 
66         /// <summary>
67         /// 增删改例子
68         /// </summary>
69         /// <param name="configName">config文件名</param>
70         /// <param name="key">key</param>
71         /// <param name="value">值</param>
72         public void ModConfig(string configName, string key, string value)
73         {
74             string configPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString() + "\\" + configName;
75             ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
76             ecf.ExeConfigFilename = configPath;
77             Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);
78 
79             config.AppSettings.Settings.Add("VersionType", "V1.5.0.8");  // 增
80             config.AppSettings.Settings[key].Value = value;              // 修改
81             config.AppSettings.Settings.Remove("VersionType");           // 删除
82             config.Save(ConfigurationSaveMode.Modified);                 // 保存文件
83 
84             ConfigurationManager.RefreshSection("appSettings");          // 重新载入使文件生效
85         }
86     }
87 }
  • 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.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.

 

补充:C#读取Web.config的不同类型的值


作者:꧁执笔小白꧂