1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176:
| using System; using System.IO; using System.Runtime.InteropServices; using System.Text;
namespace Ini { 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);
public IniFile(string INIPath) { path = INIPath; } public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.path); }
public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); return temp.ToString();
}
} }
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Ini;
namespace IniTest {
public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox name; private System.Windows.Forms.TextBox lname; private System.ComponentModel.Container components = null;
public Form1() { InitializeComponent();
}
protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code private void InitializeComponent() { this.name = new System.Windows.Forms.TextBox(); this.lname = new System.Windows.Forms.TextBox(); this.SuspendLayout(); this.name.Location = new System.Drawing.Point(8, 8); this.name.Name = "name"; this.name.Size = new System.Drawing.Size(184, 20); this.name.TabIndex = 0; this.name.Text = "Name"; this.lname.Location = new System.Drawing.Point(8, 40); this.lname.Name = "lname"; this.lname.Size = new System.Drawing.Size(184, 20); this.lname.TabIndex = 1; this.lname.Text = "Last Name"; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(208, 70); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lname, this.name}); this.Name = "Form1"; this.Text = "Form1"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false);
} #endregion
[STAThread] static void Main() { Application.Run(new Form1()); }
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); } 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"); } } } |