大家好,我想把我程序里的一些东西,保存下来,以便下次读取,本来写在ini文件里面,譬如
[APP1]
Title=Office
CopyRight=MS
Version=2K3
[APP2]
Title=SyamantecNortonVirus
CopyRight=Symantec
Version=10.0.2.2002现在我想把这个内容放在一个xml文件里面,然后写个寒暑 ,fun(section,item)就可以找到具体某个值,譬如fun("APP1","Title"),就可以返回Office,不知道可以办到不?如果可以,怎么办到呢?分不多,请帮帮忙!
[APP1]
Title=Office
CopyRight=MS
Version=2K3
[APP2]
Title=SyamantecNortonVirus
CopyRight=Symantec
Version=10.0.2.2002现在我想把这个内容放在一个xml文件里面,然后写个寒暑 ,fun(section,item)就可以找到具体某个值,譬如fun("APP1","Title"),就可以返回Office,不知道可以办到不?如果可以,怎么办到呢?分不多,请帮帮忙!
解决方案 »
- C# 用代码配置IIS
- C#进度条
- 我这册CSDN近一年了,以前我发的帖子在"我的帖子"里面找不到了,怎样才能看到我以前发过得帖子?
- 关于 DataSet 中执行 SQL 语句的问题, 对于CS_N 的垃圾搜索我很是不想再用,还不如我写的
- 有没有方法可以检索到一个窗体的所有控件
- VS IDE使用问题??
- 菜鸟windows问题?窗体调用。
- 各位老大:我想问一下用XML做为层之间数据传送参数与用其它方式比如hashtable或arraylist比起来哪个好
- 命名管道接收监听代码的问题
- 如何使用返回数组的类成员?
- 请大家给我一个使用XML Schema的理由。
- 有关如何在asp.net页面中实现在线视频聊天的问题?
*****************************************************************************
欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
using System.IO;读
string FileName = HttpContext.Current.Server.MapPath("路径");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(FileName); //get all child nodes of BannerContent
XmlNodeList nodeList = xmlDoc.SelectSingleNode("BannerContent").ChildNodes; //over all nodes
foreach (XmlNode xn in nodeList)
{
//change child node type to XmlElement
XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("Name") == "ChangeColor")
{
if (xe.GetAttribute("Visible") == "true")
{
if (us.GetFirstRoleId() != -1)
{
ChangeColorIcon = "changeicon";
DesktopPortalBanner_ChangeColor = lc.GetLanguageDescription("DesktopPortalBanner_ChangeColor");
}
}
}
}写string filePathDesktopSrc = Server.MapPath("路径");
if (!File.Exists(filePathDesktopSrc))
{
validateFileExist.ErrorMessage = BannerSrcPath.Text + " " +
lc.GetLanguageDescription("ModuleDefinitions_Error");
validateFileExist.IsValid = false;
return;
} xmlDoc.Load(FileName); //get all child nodes of BannerContent
XmlNodeList nodeList = xmlDoc.SelectSingleNode("BannerContent").ChildNodes; #region over all nodes
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Name") == "ChangeColor")
{
if (this.ManageChangeColor.Checked == true)
{
xe.SetAttribute("Visible", "true");
}
else
{
xe.SetAttribute("Visible", "false");
}
}
}
#endregion xmlDoc.Save(FileName);//save xml file
<appSettings>
<add key="SurroundingAreaIncluded" value="0"/>
<add key="WaitPerPageSEC" value="3333" />
</appSettings>在程序中ConfigurationManager.AppSettings.Get("WaitPerPageSEC").Trim();当然可以自己新建xml
<root>
<APP1 Title="Office" CopyRight="MS" Version="2K3"/>
<APP2 Title="SyamantecNortonVirus" CopyRight="Symantec" Version="10.0.2.2002"/>
</root>
有句话怎么说的来着:把时髦的技术挂在嘴边,不如把过时的技术记在心里。
Read/Write XML files, Config files, INI files, or the Registry
By Alvaro Mendez
http://www.codeproject.com/csharp/readwritexmlini.asp
注意"范围":Application则是只读的,User是运行时可修改的。
发布的时候注意加密配置文件中的敏感信息。
用户通过应用程序修改设置,设置会在
C:\Documents and Settings\用户名\Application Data\公司名
下生成一个user.config 保存用户修改的设置。
而且可以直接绑定到控件,控件里的数据=>ApplicationSettings属性里设置。
======================================
使用方法我举个例子:
=====================================
我的项目名是MyCSDN
我在项目属性设置一个名myStr,string类型, 值hello
程序里首先引用 :项目名.Properties;
例如,我们的:
using MyCSDN.Properties;然后 Settings setting = new Settings();
this.textBox1.Text=setting.myStr;//读取配置myStr
setting.Save();//保存配置
setting.Reset();//还原默认值
setting.Reload();//读取最后一次保存的值
====================
如果是绑定到控件的,窗体建立的时候会自动读取,非常方便:)