INIファイルの読み取り/書き込み 質問する

INIファイルの読み取り/書き込み 質問する

.NET フレームワークには、標準の .ini ファイルを読み書きできるクラスはありますか?

[Section]
<keyname>=<value>
...

Delphi にはTIniFileコンポーネントがありますが、C# にも同様のものがあるかどうか知りたいです。

ベストアンサー1

序文

まず、このMSDNブログ記事を読んでください。INIファイルの制限ニーズに合う場合は、読み進めてください。

これは私が書いた簡潔な実装で、オリジナルの Windows P/Invoke を利用しているので、.NET がインストールされているすべてのバージョンの Windows (Windows 98 - Windows 11) でサポートされています。私はこれをパブリック ドメインとしてリリースします。帰属表示なしで自由に商用利用できます。

小さなクラス

IniFile.csプロジェクトに次の新しいクラスを追加します。

using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

// Change this to match your program's normal namespace
namespace MyProg
{
    class IniFile   // revision 11
    {
        string Path;
        string EXE = Assembly.GetExecutingAssembly().GetName().Name;

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

        public IniFile(string IniPath = null)
        {
            Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
        }

        public string Read(string Key, string Section = null)
        {
            var RetVal = new StringBuilder(255);
            GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
            return RetVal.ToString();
        }

        public void Write(string Key, string Value, string Section = null)
        {
            WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
        }

        public void DeleteKey(string Key, string Section = null)
        {
            Write(Key, null, Section ?? EXE);
        }

        public void DeleteSection(string Section = null)
        {
            Write(null, null, Section ?? EXE);
        }

        public bool KeyExists(string Key, string Section = null)
        {
            return Read(Key, Section).Length > 0;
        }
    }
}

それの使い方

次の 3 つの方法のいずれかで INI ファイルを開きます。

// Creates or loads an INI file in the same directory as your executable
// named EXE.ini (where EXE is the name of your executable)
var MyIni = new IniFile();

// Or specify a specific name in the current dir
var MyIni = new IniFile("Settings.ini");

// Or specify a specific name in a specific dir
var MyIni = new IniFile(@"C:\Settings.ini");

次のようにいくつかの値を記述できます。

MyIni.Write("DefaultVolume", "100");
MyIni.Write("HomePage", "http://www.google.com");

次のようなファイルを作成するには:

[MyProg]
DefaultVolume=100
HomePage=http://www.google.com

INI ファイルから値を読み取るには:

var DefaultVolume = MyIni.Read("DefaultVolume");
var HomePage = MyIni.Read("HomePage");

[Section]オプションで、以下を設定できます:

MyIni.Write("DefaultVolume", "100", "Audio");
MyIni.Write("HomePage", "http://www.google.com", "Web");

次のようなファイルを作成するには:

[Audio]
DefaultVolume=100

[Web]
HomePage=http://www.google.com

次のようにしてキーの存在を確認することもできます。

if(!MyIni.KeyExists("DefaultVolume", "Audio"))
{
    MyIni.Write("DefaultVolume", "100", "Audio");
}

次のようにしてキーを削除できます。

MyIni.DeleteKey("DefaultVolume", "Audio");

次のようにして、セクション全体(すべてのキーを含む)を削除することもできます。

MyIni.DeleteSection("Web");

改善点があればお気軽にコメントしてください。

おすすめ記事