| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Linq; |
| 4 |
using System.Text; |
| 5 |
using System.Runtime.InteropServices; |
| 6 |
|
| 7 |
namespace SchoolIdolFestivalSimulator.Components { |
| 8 |
public class IniFile:IDisposable { |
| 9 |
/// <summary> |
| 10 |
/// iniファイルのパスを保持 |
| 11 |
/// </summary> |
| 12 |
private string _iniFilePath; |
| 13 |
public string IniFilePath{ |
| 14 |
get { |
| 15 |
return _iniFilePath; |
| 16 |
} |
| 17 |
} |
| 18 |
[DllImport("KERNEL32.DLL")] |
| 19 |
public static extern uint |
| 20 |
GetPrivateProfileString(string lpAppName, |
| 21 |
string lpKeyName, string lpDefault, |
| 22 |
StringBuilder lpReturnedString, uint nSize, |
| 23 |
string lpFileName); |
| 24 |
|
| 25 |
[DllImport("KERNEL32.DLL")] |
| 26 |
public static extern uint |
| 27 |
GetPrivateProfileInt(string lpAppName, |
| 28 |
string lpKeyName, int nDefault, string lpFileName); |
| 29 |
|
| 30 |
[DllImport("kernel32.dll")] |
| 31 |
private static extern int WritePrivateProfileString( |
| 32 |
string lpApplicationName, |
| 33 |
string lpKeyName, |
| 34 |
string lpstring, |
| 35 |
string lpFileName); |
| 36 |
|
| 37 |
/// <summary> |
| 38 |
/// コンストラクタ |
| 39 |
/// </summary> |
| 40 |
public IniFile(string iniFilePath) { |
| 41 |
_iniFilePath = iniFilePath; |
| 42 |
} |
| 43 |
|
| 44 |
/// <summary> |
| 45 |
/// iniファイル中のセクションのキーを指定して、文字列を取得します |
| 46 |
/// </summary> |
| 47 |
/// <param name="section">セクション</param> |
| 48 |
/// <param name="key">キー</param> |
| 49 |
/// <param name="defValue">デフォルト値</param> |
| 50 |
/// <returns></returns> |
| 51 |
public String ReadString(string section, string key,string defValue) { |
| 52 |
StringBuilder sb = new StringBuilder(1024); |
| 53 |
GetPrivateProfileString(section,key,defValue,sb,Convert.ToUInt32(sb.Capacity),_iniFilePath); |
| 54 |
return sb.ToString(); |
| 55 |
} |
| 56 |
|
| 57 |
/// <summary> |
| 58 |
/// iniファイル中のセクションのキーを指定して、整数値を取得します。 |
| 59 |
/// </summary> |
| 60 |
/// <param name="section">セクション</param> |
| 61 |
/// <param name="key">キー</param> |
| 62 |
/// <param name="defValue">デフォルト値</param> |
| 63 |
/// <returns></returns> |
| 64 |
public int ReadInteger(String section, String key,int defValue) { |
| 65 |
return (int)GetPrivateProfileInt(section, key, defValue, _iniFilePath); |
| 66 |
} |
| 67 |
|
| 68 |
/// <summary> |
| 69 |
/// 指定したセクション、キーに数値を書き込みます |
| 70 |
/// </summary> |
| 71 |
/// <param name="section"></param> |
| 72 |
/// <param name="key"></param> |
| 73 |
/// <param name="val"></param> |
| 74 |
public void WriteInteger(String section, String key, int value) { |
| 75 |
WriteString(section, key, value.ToString()); |
| 76 |
} |
| 77 |
|
| 78 |
/// <summary> |
| 79 |
/// 指定したセクション、キーに文字列を書き込みます。 |
| 80 |
/// </summary> |
| 81 |
/// <param name="section"></param> |
| 82 |
/// <param name="key"></param> |
| 83 |
/// <param name="val"></param> |
| 84 |
public void WriteString(String section, String key, String value) { |
| 85 |
WritePrivateProfileString(section, key, value, _iniFilePath); |
| 86 |
} |
| 87 |
|
| 88 |
#region IDisposable メンバー |
| 89 |
|
| 90 |
public void Dispose() { |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
#endregion |
| 95 |
} |
| 96 |
} |