程序怎么样指定app.config

.Net技术 码拜 8年前 (2016-02-25) 928次浏览
现有10个程序他们的app.config几乎是一样的而且放在同一个文件下面,怎么样让这10个程序加载同一个app.config
解决方案

5

那你可以单独做个读取配置信息的项目,暴露方法。

15

可以指定要读取的文件名

/// <summary>
/// 读取 XML 格式配置文件具体代码
/// </summary>
public static void LoadSetting()
{
    ExeConfigurationFileMap file = new ExeConfigurationFileMap();
    file.ExeConfigFilename = "xxx.config";
    Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
    ConnectionStringSettings s = cfg.ConnectionStrings.ConnectionStrings["db"];
    providername = s.ProviderName;
    connectionstring = s.ConnectionString;
    wsurl = cfg.AppSettings.Settings["wsurl"].Value;
    ftppath = cfg.AppSettings.Settings["ftppath"].Value;
    port = int.Parse(cfg.AppSettings.Settings["ftpport"].Value);
    uid = cfg.AppSettings.Settings["ftpuid"].Value;
    pwd = cfg.AppSettings.Settings["ftppwd"].Value;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明程序怎么样指定app.config
喜欢 (0)
[1034331897@qq.com]
分享 (0)