//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"); } } }