// UtilIni.cs 형태로 추가 using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace INI { class IniUtil { private string iniPath; public IniUtil(string path) { this.iniPath = path; //INI 파일 위치를 생성할때 인자로 넘겨 받음 } [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString( // GetIniValue 를 위해 String section, String key, String def, StringBuilder retVal, int size, String filePath); [DllImport("kernel32.dll")] private static extern long WritePrivateProfileString( // SetIniValue를 위해 String section, String key, String val, String filePath); // INI 값을 읽어 온다. public String GetIniValue(String Section, String Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, iniPath); return temp.ToString(); } // INI 값을 셋팅 public void SetIniValue(String Section, String Key, String Value) { WritePrivateProfileString(Section, Key, Value, iniPath); } } }
사용 예
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DCShuttle.Classes { class ConfigManager { private static string _configName = "config.ini"; public static string GetConfigPath() { System.IO.FileInfo exefileinfo = new System.IO.FileInfo(System.Windows.Forms.Application.ExecutablePath); string path = exefileinfo.Directory.FullName.ToString(); string filePath = path + "\\" + _configName; return filePath; } public static bool IsExistConfig() { string configPath = GetConfigPath(); return System.IO.File.Exists(configPath); } public static void SetDefault() { INI.IniUtil ini = new INI.IniUtil(GetConfigPath()); ini.SetIniValue("DCShuttle", "Comment", "True"); ini.SetIniValue("DCShuttle", "Sound", "True"); ini.SetIniValue("DCShuttle", "Delay", "5000"); ini.SetIniValue("DCShuttle", "Gallery", "ani1_new1"); ini.SetIniValue("DCShuttle", "StopwordsURI", "http://hyacinth.byus.net/dcs/stopwords.txt"); } public static void GetDefault() { string configPath = GetConfigPath(); if (IsExistConfig()) { INI.IniUtil ini = new INI.IniUtil(configPath); } else { SetDefault(); } } } }
[DCShuttle] Comment=False Sound=True Delay=5000
C Sharp
INI
Retrieved from http://hyacinth.byus.net/moniwiki/wiki.php/C_Sharp/INI 읽고 쓰기
last modified 2015-02-12 15:30:58