clsXMl.cs
上传用户:jiupin
上传日期:2022-04-21
资源大小:1525k
文件大小:4k
源码类别:

ListView/ListBox

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;//
  5. namespace WinListViewQQ
  6. {
  7.     class clsXMl
  8.     {
  9.         private  static XmlDocument xmlDoc = new XmlDocument();
  10.         static string fileName =System.Windows.Forms.Application.StartupPath + "\config.xml";//配置文件的路径、名
  11.         
  12.         /// <summary>
  13.         /// 返回xml文档的根节点
  14.         /// </summary>
  15.         /// <returns>根节点</returns>
  16.         public static XmlNode GetRootNode()
  17.         {
  18.             //xmlDoc = new XmlDocument();
  19.             xmlDoc.Load(fileName);
  20.             XmlNode root = xmlDoc.DocumentElement;
  21.             return root;
  22.         }
  23.         /// <summary>
  24.         ///  保存XML文件
  25.         /// </summary>
  26.        
  27.         public static void xmlSave()
  28.         {
  29.             xmlDoc.Save(fileName);
  30.         }
  31.     
  32.         /// <summary>
  33.         /// 创建节点(默认配置文件)
  34.         /// </summary>
  35.        
  36.         public static void CreateXmlConfigFile()
  37.         {
  38.             XmlTextWriter xmlWriter;
  39.             xmlWriter = new XmlTextWriter(fileName, Encoding.Default);//creat            //string fileName = Application.StartupPath + "\config.xml";
  40.             xmlWriter.Formatting = Formatting.Indented;//自动缩进格式
  41.             xmlWriter.WriteStartDocument();
  42.             xmlWriter.WriteStartElement("Root");
  43.             //
  44.                 xmlWriter.WriteStartElement("TopMost");
  45.                 xmlWriter.WriteString("yes");
  46.                 xmlWriter.WriteEndElement();
  47.                 //
  48.                 xmlWriter.WriteStartElement("AutoRun");
  49.                 xmlWriter.WriteString("no");
  50.                 xmlWriter.WriteEndElement();
  51.                 //
  52.                 xmlWriter.WriteStartElement("BgImage");
  53.                 xmlWriter.WriteString("blue");
  54.                 xmlWriter.WriteEndElement();
  55.               //
  56.                 xmlWriter.WriteStartElement("Icon");
  57.                 xmlWriter.WriteAttributeString("size", "small");
  58.                 xmlWriter.WriteString("blue");
  59.                 xmlWriter.WriteEndElement();  
  60.               
  61.             //
  62.             xmlWriter.WriteEndElement();
  63.             xmlWriter.Close();
  64.            AddXmlNode("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "我的电脑", 0, 1);
  65.            AddXmlNode("shell:ControlPanelFolder","控制面板",0,1);
  66.            AddXmlNode("regedit.exe","注册表",0,1);
  67.            AddXmlNode("Msconfig.exe","系统配置信息",0,1);
  68.            AddXmlNode("perfmon.msc","计算机性能监测",0,1);
  69.            AddXmlNode("compmgmt.msc","计算机管理",0,1);
  70.            AddXmlNode("Nslookup","IP地址侦测",0,1);
  71.            AddXmlNode("fsmgmt.msc","共享文件管理",0,1); 
  72.             //
  73.            AddXmlNode("IEXPLORE.EXE","IE",0,2);
  74.            AddXmlNode("wmplayer.exe","MediaPlay",0,2);
  75.            AddXmlNode("calc","计算器",0,2);
  76.            AddXmlNode("mspaint", "画图板", 0, 2);
  77.           ;
  78.            
  79.             
  80.                    
  81.             xmlWriter = null;
  82.             GC.Collect();
  83.         }
  84.        /// <summary>
  85.         /// 创建节点
  86.        /// </summary>
  87.         /// <param name="key">路径+程序名</param>
  88.         /// <param name="text">描述</param>
  89.         /// <param name="imageIndex">图标</param>
  90.        /// <param name="flag">组标志</param>
  91.         public static void AddXmlNode(string key,string text, int imageIndex,int flag)
  92.         {
  93.            XmlNode root= GetRootNode();
  94.            XmlElement xe = xmlDoc.CreateElement("Item");
  95.             //---
  96.                XmlElement xeFlag = xmlDoc.CreateElement("flag");
  97.                xeFlag.InnerText = flag.ToString();
  98.                xe.AppendChild(xeFlag);
  99.             //
  100.                 XmlElement xeKey = xmlDoc.CreateElement("key");
  101.                 xeKey.InnerText = key;
  102.                 xe.AppendChild(xeKey);
  103.                 //
  104.                 XmlElement xeText = xmlDoc.CreateElement("text");
  105.                 xeText.InnerText = text;
  106.                 xe.AppendChild(xeText);
  107.                 //
  108.                 XmlElement xeImageIndex = xmlDoc.CreateElement("imageIndex");
  109.                 xeImageIndex.InnerText=imageIndex.ToString();
  110.                 xe.AppendChild(xeImageIndex);
  111.                 //
  112.                
  113.            //---
  114.            root.AppendChild(xe);
  115.            xmlSave();
  116.         }
  117.     }
  118. }