| Uutiset | Koodikirjasto | Wiki | Keskustelut | FAQ | Info |
Ini tiedostojen käsittelyJari_Kettunen 06.11.05 20:33 Luokka initiedostojen käsittelyyn API-funktioiden avulla
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; 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 WritePrivateProfileSection(string section, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size,string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileSection(string section, byte[] retVal, int size, string filePath); /// <summary> /// INIFile Constructor. /// </summary> /// <PARAM name="INIPath"></PARAM> public IniFile(string INIPath) { path = INIPath; string s=Application.ExecutablePath; if (path.IndexOf("\\") == -1) { for (int i = s.Length - 1;i>0;i--) { if (s.Substring(i, 1) == "\\") { s=s.Substring(0,i+1); break; } } path = s + path; // path = Application.CommonAppDataPath + "\\" + path; } } /// <summary> /// Write Data to the INI File /// </summary> /// <PARAM name="Section"></PARAM> /// <PARAM name="Key"></PARAM> /// <PARAM name="Value"></PARAM> public void WriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); } /// <summary> /// Write Section to the INI File /// </summary> /// <PARAM name="Section"></PARAM> /// <PARAM name="Value"></PARAM> public void WriteSection(string Section, string Value) { WritePrivateProfileSection(Section, 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 ReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); return temp.ToString(); } /// <summary> /// Read Section From the Ini File /// </summary> /// <PARAM name="Section"></PARAM> /// <returns></returns> public string ReadSection(string Section) { byte[] temp = new byte[255]; int i = GetPrivateProfileSection(Section, temp, temp.Length, this.path); StringBuilder s = new StringBuilder("", temp.Length); char c=' '; for (i = 0; i < temp.Length - 1; i++) { if(temp[i]==0){ if (c==';') break; c=';'; } else c=(char)temp[i]; s.Append(c); } return s.ToString(); } } } ja käyttö vaikka seuraavasti ... using Ini; namespace Tietokannan_yllapito { public partial class FKanta : Form { //esittely ja alusutus private IniFile ini = new IniFile("kanta.ini"); ... private void FKanta_Load(object sender, EventArgs e) { string s; s=ini.ReadSection("Yhdysmerkkijono"); ... } } ... private void tallenna_Click(object sender, EventArgs e) { string s=""; foreach (string s2 in lbYhteysmerkkijono.Items) s = s + s2 + "\r\n"; ini.WriteSection("Yhdysmerkkijono", s); } .... Ztane 15:52 9.11.05 Eikö ne initiedostot saakaan maata rauhasa haudassaan :P |
![]() Haku
|