2006년 8월 28일 월요일

C#에서 INI 사용

1. 클래스

using System;

using System.Text;

using System.Runtime.InteropServices;

 

namespace Win32Tool

{

    public class IniControl

    {

      [DllImport("kernel32")]

      public static extern bool WritePrivateProfileString( string lpAppName, string lpKeyName, string lpString, string lpFileName );

       

      [DllImport("kernel32")]

      public static extern uint GetPrivateProfileInt( string lpAppName, string lpKeyName, int nDefault, string lpFileName );

       

      [DllImport("kernel32")]

      public static extern int GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName );

    }

}

 

 

2. 사용예

    string inifile = Environment.SystemDirectory + "\\Test.ini";

     

    StringBuilder result = new StringBuilder( 512 );

    Win32Tool.IniControl.GetPrivateProfileString( "색션", "키1", "error", result, 512, inifile );

    Console.WriteLine( result.ToString() );

     

    uint result2 = Win32Tool.IniControl.GetPrivateProfileInt( "색션", "키2", 0, inifile );

    Console.WriteLine( Convert.ToString( result2 ) );

     

    Win32Tool.IniControl.WritePrivateProfileString( "색션", "키3", "설정할값", inifile );

댓글 없음:

댓글 쓰기