Created
May 7, 2014 11:59
-
-
Save peace2048/0f57d291e458c0eb1a8e to your computer and use it in GitHub Desktop.
IniFile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var p = new PrivateProfile("xxx.ini"); | |
p.Section("APP").Key("Password").GetString(); | |
p.Section("APP").Key("Password").SetString("XXX"); | |
var sec = p.Section("APP"); | |
sec.Key("ABC").SetString("AAA"); | |
sec.SetString("ABC", "AAA"); | |
sec["ABC"] = "AAA"; | |
var x = sec["ABC"]; | |
} | |
} | |
class PrivateProfile | |
{ | |
public PrivateProfile(string fileName) | |
{ | |
this.FileName = fileName; | |
} | |
public string FileName { get; private set; } | |
public PrivateProfileSection Section(string name) | |
{ | |
return new PrivateProfileSection(FileName, name); | |
} | |
} | |
class PrivateProfileSection | |
{ | |
public PrivateProfileSection(string fileName, string sectionName) | |
{ | |
this.FileName = fileName; | |
this.SectionName = sectionName; | |
} | |
public string FileName { get; private set; } | |
public string SectionName { get; private set; } | |
public PrivateProfileKey Key(string name) | |
{ | |
return new PrivateProfileKey(FileName, SectionName, name); | |
} | |
public void SetString(string keyName, string value) | |
{ | |
PrivateProfileApi.WriteString(SectionName, keyName, value, FileName); | |
} | |
public string GetString(string keyName) | |
{ | |
return PrivateProfileApi.GetString(SectionName, keyName, null, FileName); | |
} | |
public string this[string keyName] | |
{ | |
get { return GetString(keyName); } | |
set { SetString(keyName, value); } | |
} | |
} | |
class PrivateProfileKey | |
{ | |
public PrivateProfileKey(string fileName, string sectionName, string keyName) | |
{ | |
this.FileName = FileName; | |
this.SectionName = sectionName; | |
this.KeyName = keyName; | |
} | |
public string FileName { get; private set; } | |
public string SectionName { get; private set; } | |
public string KeyName { get; private set; } | |
public string GetString() | |
{ | |
return PrivateProfileApi.GetString(SectionName, KeyName, null, FileName); | |
} | |
public void SetString(string value) | |
{ | |
PrivateProfileApi.WriteString(SectionName, KeyName, value, FileName); | |
} | |
} | |
/// <summary> | |
/// PrivateProfile (INIファイル) へのアクセスを提供します。 | |
/// </summary> | |
class PrivateProfileApi | |
{ | |
/// <summary> | |
/// 指定された .ini ファイル(初期化ファイル)の指定されたセクション内にある、指定されたキーに関連付けられている整数を取得します。 | |
/// </summary> | |
/// <param name="appName">.ini ファイル内のセクションの名前を指定します。</param> | |
/// <param name="keyName">キーの名前を指定します。</param> | |
/// <param name="defaultValue">指定したキーが .ini ファイル内で見つからなかったときに返すべき、既定の値を指定します。</param> | |
/// <param name="fileName">初期化ファイルの名前を指定します。ファイルのフルパス名を指定しなかった場合、システムは Windows ディレクトリ内でこのファイルを検索します。 </param> | |
/// <returns></returns> | |
public static int GetInt(string appName, string keyName, int defaultValue, string fileName) | |
{ | |
return GetPrivateProfileInt(appName, keyName, defaultValue, fileName); | |
} | |
/// <summary> | |
/// 指定された .ini ファイル(初期化ファイル)の指定されたセクション内にあるすべてのキー名を取得します。 | |
/// </summary> | |
/// <param name="appName">目的のキーが所属しているセクションの名前を指定します。</param> | |
/// <param name="fileName">初期化ファイルの名前を指定します。ファイルのフルパス名を指定しなかった場合、システムは Windows ディレクトリ内でこのファイルを検索します。 </param> | |
/// <returns></returns> | |
public static string[] GetKeyNames(string appName, string fileName) | |
{ | |
var buffer = new byte[0x10000]; | |
var len = GetPrivateProfileString(appName, null, null, buffer, buffer.Length, fileName); | |
var offset = 0; | |
var result = new List<string>(); | |
for (int i = 0; i < len; i++) | |
{ | |
if (buffer[i] == 0) | |
{ | |
result.Add(Encoding.Default.GetString(buffer, offset, i - offset)); | |
offset = i + 1; | |
} | |
} | |
return result.ToArray(); | |
} | |
/// <summary> | |
/// 指定された .ini ファイル(初期化ファイル)内のすべてのセクション名を取得します。 | |
/// </summary> | |
/// <param name="fileName">初期化ファイルの名前を指定します。ファイルのフルパス名を指定しなかった場合、システムは Windows ディレクトリ内でこのファイルを検索します。 </param> | |
/// <returns></returns> | |
public static string[] GetAppNames(string fileName) | |
{ | |
var buffer = new byte[0x10000]; | |
var len = GetPrivateProfileString(null, null, null, buffer, buffer.Length, fileName); | |
var offset = 0; | |
var result = new List<string>(); | |
for (int i = 0; i < len; i++) | |
{ | |
if (buffer[i] == 0) | |
{ | |
result.Add(Encoding.Default.GetString(buffer, offset, i - offset)); | |
offset = i + 1; | |
} | |
} | |
return result.ToArray(); | |
} | |
/// <summary> | |
/// 指定された .ini ファイル(初期化ファイル)の指定されたセクション内にある、指定されたキーに関連付けられている文字列を取得します。 | |
/// </summary> | |
/// <param name="appName">目的のキーが所属しているセクションの名前を指定します。</param> | |
/// <param name="keyName">キーの名前を指定します。</param> | |
/// <param name="defaultValue">ini ファイル内に見つからなかった場合に返す文字列</param> | |
/// <param name="fileName">初期化ファイルの名前を指定します。ファイルのフルパス名を指定しなかった場合、システムは Windows ディレクトリ内でこのファイルを検索します。 </param> | |
/// <returns></returns> | |
public static string GetString(string appName, string keyName, string defaultValue, string fileName) | |
{ | |
var buffer = new byte[0x10000]; | |
var len = GetPrivateProfileString(appName, keyName, defaultValue, buffer, buffer.Length, fileName); | |
if (len == 0 && defaultValue == null) | |
{ | |
return null; | |
} | |
return Encoding.Default.GetString(buffer, 0, len); | |
} | |
/// <summary> | |
/// 指定された .ini ファイル(初期化ファイル)の、指定されたセクション内に、指定されたキー名とそれに関連付けられた文字列を格納します。 | |
/// </summary> | |
/// <param name="appName">文字列のコピー先となるセクションの名前を指定します。このセクションが存在しない場合、新しく作成されます。このセクション名では、大文字と小文字を区別しません。文字列で、大文字と小文字の任意の組み合わせを指定できます。</param> | |
/// <param name="keyName">文字列へ関連付けるべきキーの名前を指定します。指定されたセクション内にこのキーが存在していない場合、新しく作成されます。NULL を指定すると、この関数は、そのセクション内のすべてのエントリを含めセクション全体を削除します。</param> | |
/// <param name="value">ファイルに書き込むべき文字列を指定します。NULL を指定すると、この関数は、<paramref name="keyName"/> で指定されたキーを削除します。 </param> | |
/// <param name="fileName">初期化ファイルの名前を指定します。ファイルのフルパス名を指定しなかった場合、システムは Windows ディレクトリ内でこのファイルを検索します。 </param> | |
public static void WriteString(string appName, string keyName, string value, string fileName) | |
{ | |
WritePrivateProfileString(appName, keyName, value, fileName); | |
} | |
[DllImport("kernel32", SetLastError = true)] | |
private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName); | |
[DllImport("kernel32", SetLastError = true)] | |
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, byte[] lpReturnedString, int nSize, string lpFileName); | |
[DllImport("kernel32", SetLastError = true)] | |
private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment