本文介绍了如何在从其他应用程序更新的一个应用程序中读取app.config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好我有两个应用程序说应用程序1和应用程序2,现在我的应用程序1连续读取其中使用或定义的配置文件中的值。 应用程序2更新上面的配置文件在应用程序1中声明。 我使用了委托和事件结构,其中一旦当前值获得更改订阅者获取应用程序中的通知 1 我的主要代码如下: 公开 类 Configcheck { public bool ConfigValue; // 订阅者必须实施的委托 public delegate void ConfigChangeEventHandler( object configcheck, ConfigInfoEventArgs ValueInformation); public event ConfigChangeEventHandler ConfigChange; public void Run() { for (;;) { // sleep 10毫秒 Thread.Sleep( 100 ); // 获取当前AppSetting值CHKEBL bool read_configvalue = Convert.ToBoolean(CryptorEngine.Decrypt(ConfigurationManager.AppSettings [ CHKEBL]。ToString(), true )); // 如果值已更改 // 通知订阅者 如果(read_configvalue!= ConfigValue ) { // 创建ConfigInfoEventArgs对象 // 传递给订阅者 ConfigInfoEventArgs configInformation = new ConfigInfoEventArgs(read_configvalue); // 如果有人订阅,请通知他们 if (ConfigChange!= null ) { ConfigChange( this ,configInformation); } } // 更新状态 this .ConfigValue = read_configvalue; } } } 我的问题是,当应用程序2更改应用程序1中定义的app.config文件时,上面的for循环不会读取更新的值。我需要重新启动应用程序1来读取更新的值。 所以我需要应用程序1应该在更新后立即读取更新的值申请2。请注意应用程序1和应用程序2同时运行。 我想从app.config文件中读取的值如下所示。 /> < appsettings> < add key =CHKEBLvalue =true/> < / appsettings> 应用程序2将值更改为true或false,因此相应更新配置文件的值my应用程序1应该实时读取。 先谢谢。解决方案 尝试在WPF运行时更新app.config键值 [ ^ ]。 每次请求值时,它都不会从物理文件中读取。你需要刷新部分并读取下面的值 ConfigurationManager.RefreshSection( appSettings); bool read_configvalue = Convert.ToBoolean(CryptorEngine.Decrypt (ConfigurationManager.AppSettings [ CHKEBL]。ToString(),真)); Hi I have two applications say "application 1" and "application 2", now my "application 1" continuously read value from config file that is used or defined in it. "application 2" update above config file declare in "application 1".I have used delegate and event structure where once current value get change subscriber get notification in "application 1"My main code is given bellowpublic class Configcheck { public bool ConfigValue; // the delegate the subscribers must implement public delegate void ConfigChangeEventHandler(object configcheck, ConfigInfoEventArgs ValueInformation); public event ConfigChangeEventHandler ConfigChange; public void Run() { for (; ; ) { // sleep 10 milliseconds Thread.Sleep(100); // get the current AppSetting value of "CHKEBL" bool read_configvalue = Convert.ToBoolean(CryptorEngine.Decrypt(ConfigurationManager.AppSettings["CHKEBL"].ToString(), true)); // if the Value has changed // notify the subscribers if (read_configvalue != ConfigValue) { // create the ConfigInfoEventArgs object // to pass to the subscriber ConfigInfoEventArgs configInformation = new ConfigInfoEventArgs(read_configvalue); // if anyone has subscribed, notify them if (ConfigChange != null) { ConfigChange(this, configInformation); } } // update the state this.ConfigValue = read_configvalue; } } }My problem is that when "application 2" changes the app.config file defined in "application 1", the above for loop does not read the updated value. I need to restart the "application 1" to read the updated value.so I need "application 1" should read updated value as soon as it get changed by the "application 2". please note that "application 1" and "application 2" run simultaneously.Value that I want to read from the app.config file is as given bellow.<appsettings> <add key="CHKEBL" value="true" /> </appsettings>Application 2 changes above value to true or false so accordingly that updated value of config file my application 1 should read in real time.Thanks in Advance. 解决方案 Try the method suggested at Update app.config key value at run time in WPF[^].it does not read from the physical file each time you ask for a value. you need to refresh the section and read the value as belowConfigurationManager.RefreshSection("appSettings");bool read_configvalue = Convert.ToBoolean(CryptorEngine.Decrypt(ConfigurationManager.AppSettings["CHKEBL"].ToString(), true)); 这篇关于如何在从其他应用程序更新的一个应用程序中读取app.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 23:26