lqc自用 App.Config

 try
 {
     Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(m_configName);
     string value = config.AppSettings.Settings["FactoryClass"].Value;
     if (!string.IsNullOrEmpty(value))
     {
         return value;
     }
     return string.Empty;
 }
 catch (Exception ex)
 {
     LogTy.Instance.Write(LogType.MAIN, "Factory项目配置异常:", ex);
     return string.Empty;
 }

ConfigurationManager.OpenExeConfiguration可以读取配置文件,

如果读取其他程序的配置文件。可以使用如下方法:

            Configuration con1 = ConfigurationManager.OpenExeConfiguration(@"MainApp.exe");

                if (con1.HasFile)
                {

                    AppSettingsSection appSection = (AppSettingsSection)con1.GetSection("appSettings");
                                

            }
                else
                {
                    return "";
                }

这里注意OpenExeConfiguration(string exepath) exepath指的是写入exe的路径,如上面,实际上读取的是MainApp.exe.config配置文件

如果要读取自己写的配置文件。而没有.EXE程序时候,如读取MainApp.exe.config,而没有MainApp.exe

这是可以使用

1:把配置文件MainApp.exe.config 命名为MainApp.exe.config.config

2:Configuration con1 = ConfigurationManager.OpenExeConfiguration(@"MainApp.exe.config");

                if (con1.HasFile)
                {

                    AppSettingsSection appSection = (AppSettingsSection)con1.GetSection("appSettings");
                               

            }
                else
                {
                    return "";
                }

这样就可以读取到自己的配置文件了.
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值