目录
类就是config的帮助扩展类,然后读取到的就是当前下的app.config。
1 #region 1.获取当前类库的文件配置 2 public static Configuration InitLogConfig() 3 { 4 string baesePath = AppDomain.CurrentDomain.RelativeSearchPath; 5 string fullName = Path.GetFileName(Assembly.GetCallingAssembly().Location); 6 string path = string.Format(@"{0}\{1}.config", baesePath, fullName); 7 if (File.Exists(path) == false) 8 { 9 string msg = string.Format("{0}路径下的文件未找到 ", path); 10 throw new FileNotFoundException(msg); 11 } 12 try13 { 14 ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(); 15 configFile.ExeConfigFilename = path; 16 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None); 17 return config; 18 } 19 catch (Exception ex) { throw new Exception(ex.Message); 20 } 21 } 22 #endregion
config我现在就随便写了一个 appSettings来给大家演示一下:
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="LogPath" value="E:\"/> </appSettings></configuration>
下面就是为了演示写的调用:

然后是运行结果:我们可以看到配置文件是E盘,所有我们读取到的路径也是E盘

关键字: