21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#でiniファイルを取り扱うためのクラス実装例

Posted at
InifileUtils.cs
/// <summary>
/// iniファイル取り扱いのためのユーティリティクラス
/// </summary>
class InifileUtils
{
    /// <summary>
    /// iniファイルのパスを保持
    /// </summary>
    private String filePath { get; set; }
 
    // ==========================================================
    [DllImport("KERNEL32.DLL")]
    public static extern uint
        GetPrivateProfileString(string lpAppName,
        string lpKeyName, string lpDefault,
        StringBuilder lpReturnedString, uint nSize,
        string lpFileName);
 
    [DllImport("KERNEL32.DLL")]
    public static extern uint
        GetPrivateProfileInt(string lpAppName,
        string lpKeyName, int nDefault, string lpFileName);
 
    [DllImport("kernel32.dll")]
    private static extern int WritePrivateProfileString(
        string lpApplicationName,
        string lpKeyName,
        string lpstring,
        string lpFileName);
    // ==========================================================
 
    /// <summary>
    /// コンストラクタ(デフォルト)
    /// </summary>
    public InifileUtils()
    {
        this.filePath = AppDomain.CurrentDomain.BaseDirectory + "hogehoge.ini";
    }
 
    /// <summary>
    /// コンストラクタ(fileパスを指定する場合)
    /// </summary>
    /// <param name="filePath">iniファイルパス</param>
    public InifileUtils(String filePath)
    {
        this.filePath = filePath;
    }
 
    /// <summary>
    /// iniファイル中のセクションのキーを指定して、文字列を返す
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public String getValueString(String section, String key)
    {
        StringBuilder sb = new StringBuilder(1024);
 
        GetPrivateProfileString(
            section,
            key,
            "",
            sb,
            Convert.ToUInt32(sb.Capacity),
            filePath);
 
        return sb.ToString();
    }
 
    /// <summary>
    /// iniファイル中のセクションのキーを指定して、整数値を返す
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public int getValueInt(String section, String key)
    {
        return (int)GetPrivateProfileInt(section, key, 0, filePath);
    }
 
    /// <summary>
    /// 指定したセクション、キーに数値を書き込む
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="val"></param>
    public void setValue(String section, String key, int val)
    {
        setValue(section, key, val.ToString());
    }
 
    /// <summary>
    /// 指定したセクション、キーに文字列を書き込む
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="val"></param>
    public void setValue(String section, String key, String val)
    {
        WritePrivateProfileString(section, key, val, filePath);
    }
}
21
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?