嗨罗米这里是一段代码,可以帮助你在读/右应用配置
写在
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("ModificationDate",
DateTime.Now.ToLongTimeString() + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
阅读
foreach(string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}
我的建议:如果您保存了几个不是连接字符串的设置,请使用上面的代码。只需创建一个简单的表单,创建一个字典填充它并使用网格控件显示它。
如果你只需要读/写连接字符串使用下面的代码
阅读
string cnx = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
写
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString=" your conenction string here";
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("ConnectionString");
您仍然可以使用注册表来保存这些设置,但我相信上面的代码将解决您的问题。
感谢您的回复。现在,我可以创建一个表单,作为主应用程序的一部分,或者作为一个表单部署后接受服务器名称,连接到相同的连接成功,然后使用代码将其保存到配置文件中提供了。那会是这个想法吗?对? – Romi24 2011-05-12 05:56:41
当然,为什么不呢,你可以用代码做很多事情,它真的很有用。你甚至可以保存多个连接字符串,只需用下面一行中的引号替换名称:ConfigurationManager.ConnectionStrings [“替换此”]。 – 2011-05-15 17:36:54