C# WinForm程序怎么写配置文件?
比如在Form1里有一个textbox1和buttonConfirm,Form2里面有个textbox2和buttonSet,运行的时候先运行Form2,点buttonS...
比如在Form1里有一个textbox1和buttonConfirm,Form2里面有个textbox2和buttonSet,运行的时候先运行Form2,点buttonSet跳出Form1,在textbox1输入数据,点buttonConfirm后把textbox1里的数据存到一个配置文件里app.config或者config.ini,然后textbox2里面就通过配置文件读取显示textbox1的数据。要写哪几个cs文件,具体怎么写?
展开
展开全部
//配置文件的源文件(App.Config)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServerName" value=""/>
</appSettings>
</configuration>
//更新app.config的函数
private void UpdateConfig(string Xvalue)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath+".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value",Xvalue);
doc.Save(Application.ExecutablePath+".config");
}
web.config:
/// <summary>
/// 向.config文件的appKey结写入信息AppValue 保存设置
/// </summary>
/// <param name="AppKey">节点名</param>
/// <param name="AppValue">值</param>
Private void SetValue(String AppKey,String AppValue)
{
Xmldocument xDoc=new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath+”.config”);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode=xDoc.SelectSingleNode(“//appSettings”);
xElem1=(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”);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengfeili/archive/2008/04/19/2306407.aspx
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServerName" value=""/>
</appSettings>
</configuration>
//更新app.config的函数
private void UpdateConfig(string Xvalue)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath+".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value",Xvalue);
doc.Save(Application.ExecutablePath+".config");
}
web.config:
/// <summary>
/// 向.config文件的appKey结写入信息AppValue 保存设置
/// </summary>
/// <param name="AppKey">节点名</param>
/// <param name="AppValue">值</param>
Private void SetValue(String AppKey,String AppValue)
{
Xmldocument xDoc=new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath+”.config”);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode=xDoc.SelectSingleNode(“//appSettings”);
xElem1=(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”);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengfeili/archive/2008/04/19/2306407.aspx
展开全部
读写INI文件
public class IniFile
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="Value">值</param>
/// <param name="filepath">INI文件路径</param>
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="filepath">INI文件路径</param>
/// <returns>值</returns>
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();
}
}
public class IniFile
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="Value">值</param>
/// <param name="filepath">INI文件路径</param>
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">关键字</param>
/// <param name="filepath">INI文件路径</param>
/// <returns>值</returns>
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
解析XML
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没看懂,一楼大虾能再给点注释么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询