winform 程序中读写app.config
2011-10-11 09:27
288 查看
在form项目中新建一个app.config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source = 。;user id = sa;password = 123456;Initial Catalog = bestoas" />
</appSettings>
</configuration>
Model层
public ConnectionStringInfo() { }
private string _dataBaseIP;
private string _dataBaseName;
private string _dataBaseUid;
private string _dataBasePwd;
public string DataBaseIP {get; set;}
public string DataBaseName { get; set; }
public string DataBaseUid { get; set; }
public string DataBasePwd { get; set; }
主要思路是修改xml文件 ,代码放到了DbHelperSQL中
添加引用:System.Windows.Forms;
/// <summary>
/// 设置配置文件 对指定项设置指定值
/// </summary>
/// <param name="AppKey"></param>
/// <param name="AppValue"></param>
public static bool SetValue(string AppKey, string AppValue)
{
bool IsSuccessful = false;
try
{
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
System.Xml.XmlNode xNode;
System.Xml.XmlElement xElem1;
System.Xml.XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
IsSuccessful = true;
return IsSuccessful;
}
catch (Exception ex)
{
Console.Write(ex.Message);
throw ex;
}
return IsSuccessful;
}
DAL层
public bool UpdateConfigFile(ConnectionStringInfo connstrinfo)
{
string connectionStringValue = "";
connectionStringValue = String.Format("Data Source = '{0}';user id = '{1}';password = '{2}';Initial Catalog = '{3}'",connstrinfo.DataBaseIP,connstrinfo.DataBaseUid,connstrinfo.DataBasePwd,connstrinfo.DataBaseName);
bool stauts= DbHelperSQL.SetValue("ConnectionString",connectionStringValue);
return stauts;
}
UI层
ConnectionStringInfo connectionstringinfo = new ConnectionStringInfo();
connectionstringinfo.DataBaseIP = txtDataBaseIP.Text;
connectionstringinfo.DataBaseName = txtDataBaseName.Text;
connectionstringinfo.DataBasePwd = txtDataBaseUpwd.Text;
connectionstringinfo.DataBaseUid = txtDataBaseUid.Text;
bool stauts = new ConnectionStringManager().UpdateConfigFile(connectionstringinfo);
编译后会多三个文件
.vshost.exe
.vshost.exe.config
.vshost.exe.manifest
关于vshost.exe的解释:
http://baike.baidu.com/view/1587877.htm
宿主进程是 Visual Studio 2005/2008/2010 中的一项功能,能提高调试性能,支持部分信任调试并支持设计时表达式计算。宿主进程文件的文件名中包含 vshost,并位于项目的输出文件夹中。
●提高的调试性能 宿主进程创建一个应用程序域并将调试器与应用程序关联起来。执行这些任务会导致在开始调试和开始运行应用程序之间有很明显的延迟。通过在后台创建应用程序域和关联调试器,并在运行应用程序之间保存应用程序域和调试器状态,宿主进程使性能得到提高。
●部分信任调试 在“项目设计器”的安全页中,可将应用程序指定为部分信任应用程序。调试部分信任应用程序需要对应用程序域进行特殊的初始化。此初始化由宿主进程处理。
●设计时表达式计算 设计时表达式计算使您可以从“即时”窗口进行代码测试,而不必运行应用程序。宿主进程在设计时表达式计算期间执行此代码。
启用调试后,修改的数据保存到了.vshost.exe.config中了。所以实际部署的时候,去掉下面的三个文件就可以了
.vshost.exe
.vshost.exe.config
.vshost.exe.manifest
去掉之后,修改数据库连接保存到了app.config这个文件中。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source = 。;user id = sa;password = 123456;Initial Catalog = bestoas" />
</appSettings>
</configuration>
Model层
public ConnectionStringInfo() { }
private string _dataBaseIP;
private string _dataBaseName;
private string _dataBaseUid;
private string _dataBasePwd;
public string DataBaseIP {get; set;}
public string DataBaseName { get; set; }
public string DataBaseUid { get; set; }
public string DataBasePwd { get; set; }
主要思路是修改xml文件 ,代码放到了DbHelperSQL中
添加引用:System.Windows.Forms;
/// <summary>
/// 设置配置文件 对指定项设置指定值
/// </summary>
/// <param name="AppKey"></param>
/// <param name="AppValue"></param>
public static bool SetValue(string AppKey, string AppValue)
{
bool IsSuccessful = false;
try
{
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
System.Xml.XmlNode xNode;
System.Xml.XmlElement xElem1;
System.Xml.XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
IsSuccessful = true;
return IsSuccessful;
}
catch (Exception ex)
{
Console.Write(ex.Message);
throw ex;
}
return IsSuccessful;
}
DAL层
public bool UpdateConfigFile(ConnectionStringInfo connstrinfo)
{
string connectionStringValue = "";
connectionStringValue = String.Format("Data Source = '{0}';user id = '{1}';password = '{2}';Initial Catalog = '{3}'",connstrinfo.DataBaseIP,connstrinfo.DataBaseUid,connstrinfo.DataBasePwd,connstrinfo.DataBaseName);
bool stauts= DbHelperSQL.SetValue("ConnectionString",connectionStringValue);
return stauts;
}
UI层
ConnectionStringInfo connectionstringinfo = new ConnectionStringInfo();
connectionstringinfo.DataBaseIP = txtDataBaseIP.Text;
connectionstringinfo.DataBaseName = txtDataBaseName.Text;
connectionstringinfo.DataBasePwd = txtDataBaseUpwd.Text;
connectionstringinfo.DataBaseUid = txtDataBaseUid.Text;
bool stauts = new ConnectionStringManager().UpdateConfigFile(connectionstringinfo);
编译后会多三个文件
.vshost.exe
.vshost.exe.config
.vshost.exe.manifest
关于vshost.exe的解释:
http://baike.baidu.com/view/1587877.htm
宿主进程是 Visual Studio 2005/2008/2010 中的一项功能,能提高调试性能,支持部分信任调试并支持设计时表达式计算。宿主进程文件的文件名中包含 vshost,并位于项目的输出文件夹中。
●提高的调试性能 宿主进程创建一个应用程序域并将调试器与应用程序关联起来。执行这些任务会导致在开始调试和开始运行应用程序之间有很明显的延迟。通过在后台创建应用程序域和关联调试器,并在运行应用程序之间保存应用程序域和调试器状态,宿主进程使性能得到提高。
●部分信任调试 在“项目设计器”的安全页中,可将应用程序指定为部分信任应用程序。调试部分信任应用程序需要对应用程序域进行特殊的初始化。此初始化由宿主进程处理。
●设计时表达式计算 设计时表达式计算使您可以从“即时”窗口进行代码测试,而不必运行应用程序。宿主进程在设计时表达式计算期间执行此代码。
注意
宿主进程文件 (.vshost.exe) 由 Visual Studio 2005 使用,不应通过应用程序直接运行或部署。启用调试后,修改的数据保存到了.vshost.exe.config中了。所以实际部署的时候,去掉下面的三个文件就可以了
.vshost.exe
.vshost.exe.config
.vshost.exe.manifest
去掉之后,修改数据库连接保存到了app.config这个文件中。
相关文章推荐
- Winform读写App.config文件以及重启程序
- Winform读写App.config文件以及重启程序
- Winform读写App.config文件以及重启程序
- Winform读写App.config文件以及重启程序
- winform客户端程序实时读写app.config文件
- 在winform中使用程序读取和修改App.config里面的appSettings当中的Value值
- C#winform 程序,代码修改app.config
- winform 程序的配置文件——App.config(转)
- winform程序读取和改写配置文件App.config元素的值
- Winform中C#实现读写app.config中的connectionStrings
- [问]VS2005,C#winform程序,代码修改app.config的结果保存到哪里了?
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
- c#,WinForm中读写配置文件App.config
- winform程序读取和改写配置文件App.config元素的值
- c#,WinForm中读写配置文件App.config
- c# winform app.config xml 配置文件 读写操作
- C# WinForm程序的App.Config数据库连接配置文件
- winform程序读取和改写配置文件App.config元素的值
- C# WinForm程序的App.Config数据库连接配置文件
- winform 程序的配置文件——App.config