加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

wince xml 配置文件设置,直接上源码

发布时间:2020-12-16 05:14:42 所属栏目:百科 来源:网络整理
导读:public static class ConfigManager { private static string configName="app.xml"; private static string XMLPath = ""; private static XmlDocument xmlIAUConfig; public static XmlDocument XMLIAUConfig { get { return xmlIAUConfig; } set { xmlIAU

public static class ConfigManager
{
private static string configName="app.xml";
private static string XMLPath = "";
private static XmlDocument xmlIAUConfig;
public static XmlDocument XMLIAUConfig
{
get { return xmlIAUConfig; }
set { xmlIAUConfig = value; }
}
private static string platForm;
private static string PlatForm
{
get
{
return Environment.OSVersion.Platform.ToString();
}
}
private static string currentPath;
public static string CurrentPath
{
get
{
if (PlatForm.Equals("WinCE"))
{
currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
}
else if (PlatForm.Equals("Win32NT"))
{
currentPath = Directory.GetCurrentDirectory();
}

return currentPath;
}
}

static ConfigManager()
{
xmlIAUConfig = new XmlDocument();
XMLPath = CurrentPath;
Int32 i = XMLPath.LastIndexOf("/");
if (i >= 0)
XMLPath = XMLPath.Remove(i);
XMLPath = XMLPath + "//"+configName;
xmlIAUConfig.Load(XMLPath);
}

public static String GetValue(String key)
{
xmlIAUConfig.Load(XMLPath);
String value;
String path = @"//DataAccess/appSettings/add[@key='" + key + "']";
XmlNodeList xmlAdds = xmlIAUConfig.SelectNodes(path);

if (xmlAdds.Count == 1)
{
XmlElement xmlAdd = (XmlElement)xmlAdds[0];

value = xmlAdd.GetAttribute("value");
}
else
{
throw new Exception("IAUConfig配置信息设置错误:键值为" + key + "的元素不等于1");
}

return value;
}
public static void SavaConfig(string strKey,string strValue)
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(configName);
XmlNodeList list = XMLDoc.GetElementsByTagName("add");

for (int i = 0; i < list.Count; i++) { if (list[i].Attributes[0].Value == strKey) { list[i].Attributes[1].Value = strValue; } } StreamWriter swriter = new StreamWriter(configName); XmlTextWriter xw = new XmlTextWriter(swriter); xw.Formatting = Formatting.Indented; XMLDoc.WriteTo(xw); xw.Close(); swriter.Close(); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读