华域联盟 .Net .net 读取非标准配置文件的小例子

.net 读取非标准配置文件的小例子

代码如下:

复制代码 代码如下:

public static string Config(string key) 

       { 

           ExeConfigurationFileMap file = new ExeConfigurationFileMap(); 

           file.ExeConfigFilename = @"Providers\\Provider.config"; 

           Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); 

           AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings"); 

           return appsection.Settings[key].Value; 

       } 

配置文件目录结构:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?> 

<configuration> 

    <appSettings> 

        <add key="ConnectionString" value="Server=(local);Database=DB;User Id=sa;Password=123" /> 

    </appSettings> 

</configuration> 

调用:

复制代码 代码如下:

//里面的参数为配置文件的key  

string strConn=Config("ConnectionString"); 
您可能感兴趣的文章:

  • asp.net 在处理向该请求提供服务所需的配置文件时出错
  • .NET 2.0获取配置文件AppSettings和ConnectionStrings节数据的方法
  • 认识ASP.NET配置文件Web.config

本文由 华域联盟 原创撰写:华域联盟 » .net 读取非标准配置文件的小例子

转载请保留出处和原文链接:https://www.cnhackhy.com/51455.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部