C# ini文件与xml文件读写入门

//ini文件部分

using System; using System.IO; using System.Runtime.InteropServices; using System.Text;

namespace Ini {  /// <summary>  /// Create a New INI file to store or load data  /// </summary>  public class IniFile  {   public string path;

  [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>   /// INIFile Constructor.   /// </summary>   /// <param name="INIPath"></param>   public IniFile(string INIPath)   {    path = INIPath;   }   /// <summary>   /// Write Data to the INI File   /// </summary>   /// <param name="Section"></param>   /// Section name   /// <param name="Key"></param>   /// Key Name   /// <param name="Value"></param>   /// Value Name   public void IniWriteValue(string Section,string Key,string Value)   {    WritePrivateProfileString(Section,Key,Value,this.path);   }      /// <summary>   /// Read Data Value From the Ini File   /// </summary>   /// <param name="Section"></param>   /// <param name="Key"></param>   /// <param name="Path"></param>   /// <returns></returns>   public string IniReadValue(string Section,string Key)   {    StringBuilder temp = new StringBuilder(255);    int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);    return temp.ToString();

  }  } } /* In your project namespace definition add  using INI; Create a INIFile like this

INIFile ini = new INIFile("C://test.ini"); Use IniWriteValue to write a new value to a specific key in a section or use IniReadValue to read a value FROM a key in a specific Section. */使用如下:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)   {    IniFile ini = new IniFile("C://test.ini");    ini.IniWriteValue("Info","Name",name.Text);    ini.IniWriteValue("Info","LastName",lname.Text);             ini.IniWriteValue("Infos", "LastName", lname.Text);   }   private void Form1_Load(object sender, System.EventArgs e)   {    IniFile ini = new IniFile("C://test.ini");    name.Text= ini.IniReadValue("Info","Name");    lname.Text = ini.IniReadValue("Info","LastName");   }

 

//xml文件部分

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;

using System.Xml;

namespace WindowsApplication {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         private string path = @"person.xml";         private void button1_Click(object sender, EventArgs e)         {             try             {                 XmlWriterSettings xmlset = new XmlWriterSettings();                 xmlset.Indent = true;                 xmlset.IndentChars = "   ";

                XmlWriter writer = XmlWriter.Create(path, xmlset);

                writer.WriteStartElement("person");

                writer.WriteElementString("name", this.textBox1.Text);

                writer.WriteElementString("age", this.textBox2.Text);

                writer.WriteElementString("sex", this.textBox3.Text);

                writer.WriteEndElement();                 writer.Flush();                 writer.Close();             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);                 return;             }             MessageBox.Show("Write XML Successfully");         }

        private void Form1_Load(object sender, EventArgs e)         {

        }

        private void button2_Click(object sender, EventArgs e)         {             try             {                 XmlTextReader reader = new XmlTextReader(path);                 while (reader.Read())                 {

                    if (reader.IsEmptyElement)                     {                         MessageBox.Show(reader.LocalName + " has no data!");                     }                     if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "name")                     {                         this.textBox1.Text = reader.ReadString();                     }                     if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "age")                     {                         this.textBox2.Text = reader.ReadString();                     }                     if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "sex")                     {                         this.textBox3.Text = reader.ReadString();                     }                 }                 reader.Close();             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);                 return;             }             MessageBox.Show("Read XML Successfully");         }     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值