如何在运行时更新app.config然后从中读取? [英] How to update app.config in runtime and then read from it ?

查看:83
本文介绍了如何在运行时更新app.config然后从中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想在其中更新app.config。然后从中读取。但它没有发生,我得到的先前值不是更新的值。我的代码是这样的:

  class 程序
{
static string str = ConfigurationManager.AppSettings [ 用户ID]; // 它返回hello
static string key = UserID;
static string value =的NewValue();
static string exeFileName = ConsoleApplication.exe;
静态 void Main( string [] args)
{
Console.WriteLine(str);
UpdateConfig(key, value ,exeFileName);
str = ConfigurationManager.AppSettings [ UserID];
Console.WriteLine(str);
// 做其他事情
}
private static string NewValue()
{
return HelloWorld;
}
私有 静态 void UpdateConfig( string 键, string value string exeFileName)
{
try
{
配置configFile = ConfigurationManager.OpenExeConfiguration(exeFileName);
configFile.AppSettings.Settings [key] .Value = value ;
configFile.Save();
}
catch (例外情况)
{
throw ex;
}
}
// 其他一些功能
}





两次我打印你好而不是HelloWorld。

请帮助我。

提前谢谢。

解决方案





试试这样..

  //  更改UpdateConfig功能如下。 
private static void UpdateConfig( string key, string value
{
try
{
配置configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None );
configFile.AppSettings.Settings [key] .Value = value ;
configFile.Save();
}
catch (例外情况)
{
throw ex;
}
}





而不是exefilename,请尝试使用 ConfigurationUserLevel.None



参考 ConfigurationManager.OpenExeConfiguration方法(字符串) - MSDN [ ^ ]



希望它有所帮助。


< blockquote>嗨试试这个,



 Key = txtKServerName.Text; 
Value = txtVServerName.Text;
XmlNode appSettingsNode = xmlDoc.SelectSingleNode( configuration / appSettings);
尝试
{
if (KeyExists(Key))
{
MessageBox.Show( 键名: + Key + 已存在于配置中。);
}
else
{
XmlNode newChild = appSettingsNode.FirstChild.Clone();
newChild.Attributes [ key]。Value = Key;
newChild.Attributes [ value]。Value = Value;
appSettingsNode.AppendChild(newChild);
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + .. \\..\\\ \\App.config);
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
MessageBox.Show( 成功添加);
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}


此代码将编辑您的app.config:

  var  cfg = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly()。Location); 
cfg.AppSettings.Settings.Add( test1 test2);
cfg.Save();



确保在查找更改时检查正确的配置文件。不是项目中的那个,而是bin文件夹中的那个;)


I''ve a application in which I want to update app.config. And then read from it. But it''s not happening, I''m getting the prior value not the updated value. My code like this :

class Program
    {
	static string str= ConfigurationManager.AppSettings["UserID"];	// it returns "hello"
        static string key = "UserID";
        static string value = NewValue();
        static string exeFileName = "ConsoleApplication.exe";
        static void Main(string[] args)
        {
                 Console.WriteLine(str);		
                 UpdateConfig(key, value, exeFileName);
                str= ConfigurationManager.AppSettings["UserID"];
		Console.WriteLine(str);
		// Doing other stuff
	}
	private static string NewValue()
	{
		return "HelloWorld";
	}
	private static void UpdateConfig(string key, string value, string exeFileName)
        {
            try
            {
                Configuration configFile = ConfigurationManager.OpenExeConfiguration(exeFileName);
                configFile.AppSettings.Settings[key].Value = value;
                configFile.Save();
            }
            catch (Exception ex)
            { 
                throw ex; 
            }
        }
	// Here some others functions
   }



Both time I''m printing "hello" not "HelloWorld".
Please Help me.
Thanks in advance.

解决方案

Hi,

try like this..

// change the UpdateConfig function like below.
private static void UpdateConfig(string key, string value)
{
    try
    {
        Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        configFile.AppSettings.Settings[key].Value = value;
        configFile.Save();
    }
    catch (Exception ex)
    { 
        throw ex; 
    }
}



instead of exefilename, try by using ConfigurationUserLevel.None.

refer ConfigurationManager.OpenExeConfiguration Method (String) - MSDN[^]

hope it helps.


hi try this,

Key = txtKServerName.Text;
                    Value = txtVServerName.Text;
                   XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
            try
            {
                if (KeyExists(Key))
                {
                    MessageBox.Show("Key name:" + Key + "already exists in the configuration.");
                }
                else
                {
                    XmlNode newChild = appSettingsNode.FirstChild.Clone();
                    newChild.Attributes["key"].Value = Key;
                    newChild.Attributes["value"].Value = Value;
                    appSettingsNode.AppendChild(newChild);
                    xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
                    xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                    MessageBox.Show("Added Successfully ");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


This code will edit your app.config:

var cfg = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
cfg.AppSettings.Settings.Add("test1", "test2");
cfg.Save();


Make sure you check tha correct config file when you look for changes. Not the one in your project, but the one in your bin folder ;)


这篇关于如何在运行时更新app.config然后从中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆