当前位置: 移动技术网 > IT编程>开发语言>.net > 【Config】类库读取自己的配置文件,配置文件的扩展

【Config】类库读取自己的配置文件,配置文件的扩展

2018年09月28日  | 移动技术网IT编程  | 我要评论

高邮市邮编,异世之恶魔猎手,快乐男声冠军之夜

    我们在项目中一般都是使用统一的项目文件配置,所有的配置和自定义的字段都写在一个web.config或者app.config文件中。一般平时我们也没有发现问题,确实这么写没有问题,但是就是如果写的多了就看着很臃肿。

并且假如你其他地方不是主项目的配置也写在这里,多了是不是很乱,有时候自己都不知道这个是配置的那个东西了。这不我在搭建自己的框架做日志写入的时候就发现这个问题,我就想因为我日志写入我是单独作为类库存在的,所以我不一定非要写在web.config中啊。我直接写在我类库下的配置文件就可以啊。想法不错说干就干,经过一下午的鼓捣吧(其实也弄别的拉。)终于封装成了一个方法其实很简单,主要是老是报路径错误鼓捣的时间。原本我是想用程序集的路径(assembly.location或者assembly.codebase)不过研究半天发现他们给的路径都不好使,也不知道为什么(回头再研究先解决眼下的问题)。反正是一个给返回c盘的路径一个是有多于字段。最后我还是用appdomain这个代替吧,先用着发现不行在撤。嘻嘻嘻

最后大概就是这么一个结构:

类就是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             try
13             {
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盘

 

 走到这里就说明方法成功了。我很开心哈哈。想法实现了。

注意:这个扩展是那个方法调用所在的项目或者类库的配置文件不是不变的。

获取当前指定位置文件配置

写完这个之后我又想到一个想法那就是指定读取项目中类库的配置文件,有了第一个的基础,这个还不是手到擒来。所有代码就改成这样了:

 1  #region 2.获取当前指定位置文件配置
 2         public static configuration initlogconfig2(string fullname)
 3         {
 4             string baesepath = appdomain.currentdomain.relativesearchpath;
 5 
 6             string path = string.format(@"{0}\{1}.dll.config", baesepath, fullname);
 7             if (file.exists(path) == false)
 8             {
 9                 string msg = string.format("{0}路径下的文件未找到 ", path);
10                 throw new filenotfoundexception(msg);
11             }
12             try
13             {
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)
20             {
21                 throw new exception(ex.message);
22             }
23 
24         }

为了演示我就这样子写一下吧,不演示一下在不明白区别:

运行起来效果:

可以看到一个是类库的e盘一个是我主文件写的d盘,说明成功了。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网