C#에서 ini 파일을 R/W 하기 위해서는 비관리 함수를 extern으로 가져와야 한다.

 

#region DllImport 사용 ini 파일에서 정보 가져오거나 Write하기
public class iniRW
{
public static string m_sFilePath = @"C:\TEST\Table.ini";
[DllImport("kernel32.dll")]
public static extern int GetPrivateProfileString(
string section,
string key,
string def,
StringBuilder retVal,
int nSize,
string lpFilePath);

[DllImport("kernel32.dll")]
public static extern long WritePrivateProfileString(
string section,
string key,
string val,
string lpFilePath);
}

#endregion

 

 

// ReadINI
public string ReadINI(string sID, string sKey, string sDef="")
{
if (!System.IO.File.Exists(iniRW.m_sFilePath)) return null;

StringBuilder sb = new StringBuilder(1024);
iniRW.GetPrivateProfileString(sID, sKey, sDef, sb, sb.Capacity, iniRW.m_sFilePath);
return sb.ToString();
}

 

// WriteINI
public bool WriteINI(string sID, string sKey, string sVal)
{
if (!System.IO.File.Exists(iniRW.m_sFilePath)) return false;
iniRW.WritePrivateProfileString(sID, sKey, sVal, iniRW.m_sFilePath);
return true;
}

 

 

+ Recent posts