Home
Manage Your Code
Snippet: read and write INI file (C#)
Title: read and write INI file Language: C#
Description: how to read or write ini file using .net? Views: 554
Author: Kevin zhang Date Added: 12/3/2007
Copy Code  
1public class IniFile {
2		
3		private string Path;
4		[DllImport("kernel32")]
5		private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
6
7		[DllImport("kernel32")]
8		private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
9		private bool fileExist;
10
11		/// <summary>

12		/// ???ini??

13		/// </summary>

14		/// <param name="iniName">ini?????</param>

15		public IniFile(string iniName) {
16			Path = iniName;
17			fileExist=System.IO.File.Exists(Path);
18		}
19		/// <summary>

20		/// ??:????????

21		/// </summary>

22		public bool FileExist {
23			get{return fileExist;}
24		}
25
26		/// <summary>

27		/// ?ini??

28		/// </summary>

29		/// <param name="Section">Section</param>

30		/// <param name="Key">Key</param>

31		/// <param name="Value">Value</param>

32		public void WriteValue(string Section,string Key,string Value) {
33			WritePrivateProfileString(Section,Key,Value,this.Path);
34
35		}
36
37		/// <summary>

38		/// ?ini??

39		/// </summary>

40		/// <param name="Section">Section</param>

41		/// <param name="Key">Key</param>

42		/// <returns>Key?value</returns>

43		public string ReadValue(string Section,string Key) {
44			StringBuilder temp = new StringBuilder(255);
45			int i = GetPrivateProfileString(Section,Key,"",temp,255,this.Path);
46			return temp.ToString();
47
48		}
49	}
Usage
read or write ini file