284

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

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

Delphi にはTIniFileコンポーネントがあり、C# に類似したものがあるかどうか知りたいですか?

4

16 に答える 16

260

序文

まず、 INI ファイルの制限に関するこの MSDN ブログ投稿をお読みください。ニーズに合っている場合は、読み進めてください。

これは、元の Windows P/Invoke を利用して私が書いた簡潔な実装であるため、.NET がインストールされている Windows のすべてのバージョン (つまり、Windows 98 - Windows 10) でサポートされています。私はここにそれをパブリック ドメインに解放します - 帰属なしで自由に商業的に使用できます。

小さなクラス

プロジェクトに呼び出される新しいクラスを追加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");

改善点があれば遠慮なくコメントしてください!

于 2013-02-16T02:52:39.737 に答える
201

.NET フレームワークの作成者は、INI ファイルではなく、XML ベースの構成ファイルを使用することを望んでいます。いいえ、それらを読み取るための組み込みのメカニズムはありません。

ただし、利用可能なサードパーティのソリューションがあります。

于 2008-10-20T09:42:20.923 に答える
66

CodeProject に関するこの記事「C# を使用した INI ファイル処理クラス」が役立ちます。

著者は、KERNEL32.dll から 2 つの関数を公開する C# クラス「Ini」を作成しました。これらの機能は次のとおり WritePrivateProfileStringですGetPrivateProfileStringSystem.Runtime.InteropServicesとの 2 つの名前空間が必要ですSystem.Text

Ini クラスを使用する手順

プロジェクトの名前空間定義に追加します

using INI;

このようなINIFileを作成します

INIFile ini = new INIFile("C:\\test.ini");

IniWriteValueセクション内の特定のキーに新しい値を書き込むために使用するかIniReadValue、特定のセクション内のキーから値を読み取るために使用します。

注: ゼロから始める場合は、このMSDN の記事を読むことができます: How to: Add Application Configuration Files to C# Projects . これは、アプリケーションを構成するためのより良い方法です。

于 2008-10-20T09:42:55.993 に答える
49

私はこの単純な実装を見つけました:

http://bytes.com/topic/net/insights/797169-reading-parsing-ini-file-c

私が必要とするもののためにうまくいきます。

使用方法は次のとおりです。

public class TestParser
{
    public static void Main()
    {
        IniParser parser = new IniParser(@"C:\test.ini");

        String newMessage;

        newMessage = parser.GetSetting("appsettings", "msgpart1");
        newMessage += parser.GetSetting("appsettings", "msgpart2");
        newMessage += parser.GetSetting("punctuation", "ex");

        //Returns "Hello World!"
        Console.WriteLine(newMessage);
        Console.ReadLine();
    }
}

コードは次のとおりです。

using System;
using System.IO;
using System.Collections;

public class IniParser
{
    private Hashtable keyPairs = new Hashtable();
    private String iniFilePath;

    private struct SectionPair
    {
        public String Section;
        public String Key;
    }

    /// <summary>
    /// Opens the INI file at the given path and enumerates the values in the IniParser.
    /// </summary>
    /// <param name="iniPath">Full path to INI file.</param>
    public IniParser(String iniPath)
    {
        TextReader iniFile = null;
        String strLine = null;
        String currentRoot = null;
        String[] keyPair = null;

        iniFilePath = iniPath;

        if (File.Exists(iniPath))
        {
            try
            {
                iniFile = new StreamReader(iniPath);

                strLine = iniFile.ReadLine();

                while (strLine != null)
                {
                    strLine = strLine.Trim().ToUpper();

                    if (strLine != "")
                    {
                        if (strLine.StartsWith("[") && strLine.EndsWith("]"))
                        {
                            currentRoot = strLine.Substring(1, strLine.Length - 2);
                        }
                        else
                        {
                            keyPair = strLine.Split(new char[] { '=' }, 2);

                            SectionPair sectionPair;
                            String value = null;

                            if (currentRoot == null)
                                currentRoot = "ROOT";

                            sectionPair.Section = currentRoot;
                            sectionPair.Key = keyPair[0];

                            if (keyPair.Length > 1)
                                value = keyPair[1];

                            keyPairs.Add(sectionPair, value);
                        }
                    }

                    strLine = iniFile.ReadLine();
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (iniFile != null)
                    iniFile.Close();
            }
        }
        else
            throw new FileNotFoundException("Unable to locate " + iniPath);

    }

    /// <summary>
    /// Returns the value for the given section, key pair.
    /// </summary>
    /// <param name="sectionName">Section name.</param>
    /// <param name="settingName">Key name.</param>
    public String GetSetting(String sectionName, String settingName)
    {
        SectionPair sectionPair;
        sectionPair.Section = sectionName.ToUpper();
        sectionPair.Key = settingName.ToUpper();

        return (String)keyPairs[sectionPair];
    }

    /// <summary>
    /// Enumerates all lines for given section.
    /// </summary>
    /// <param name="sectionName">Section to enum.</param>
    public String[] EnumSection(String sectionName)
    {
        ArrayList tmpArray = new ArrayList();

        foreach (SectionPair pair in keyPairs.Keys)
        {
            if (pair.Section == sectionName.ToUpper())
                tmpArray.Add(pair.Key);
        }

        return (String[])tmpArray.ToArray(typeof(String));
    }

    /// <summary>
    /// Adds or replaces a setting to the table to be saved.
    /// </summary>
    /// <param name="sectionName">Section to add under.</param>
    /// <param name="settingName">Key name to add.</param>
    /// <param name="settingValue">Value of key.</param>
    public void AddSetting(String sectionName, String settingName, String settingValue)
    {
        SectionPair sectionPair;
        sectionPair.Section = sectionName.ToUpper();
        sectionPair.Key = settingName.ToUpper();

        if (keyPairs.ContainsKey(sectionPair))
            keyPairs.Remove(sectionPair);

        keyPairs.Add(sectionPair, settingValue);
    }

    /// <summary>
    /// Adds or replaces a setting to the table to be saved with a null value.
    /// </summary>
    /// <param name="sectionName">Section to add under.</param>
    /// <param name="settingName">Key name to add.</param>
    public void AddSetting(String sectionName, String settingName)
    {
        AddSetting(sectionName, settingName, null);
    }

    /// <summary>
    /// Remove a setting.
    /// </summary>
    /// <param name="sectionName">Section to add under.</param>
    /// <param name="settingName">Key name to add.</param>
    public void DeleteSetting(String sectionName, String settingName)
    {
        SectionPair sectionPair;
        sectionPair.Section = sectionName.ToUpper();
        sectionPair.Key = settingName.ToUpper();

        if (keyPairs.ContainsKey(sectionPair))
            keyPairs.Remove(sectionPair);
    }

    /// <summary>
    /// Save settings to new file.
    /// </summary>
    /// <param name="newFilePath">New file path.</param>
    public void SaveSettings(String newFilePath)
    {
        ArrayList sections = new ArrayList();
        String tmpValue = "";
        String strToSave = "";

        foreach (SectionPair sectionPair in keyPairs.Keys)
        {
            if (!sections.Contains(sectionPair.Section))
                sections.Add(sectionPair.Section);
        }

        foreach (String section in sections)
        {
            strToSave += ("[" + section + "]\r\n");

            foreach (SectionPair sectionPair in keyPairs.Keys)
            {
                if (sectionPair.Section == section)
                {
                    tmpValue = (String)keyPairs[sectionPair];

                    if (tmpValue != null)
                        tmpValue = "=" + tmpValue;

                    strToSave += (sectionPair.Key + tmpValue + "\r\n");
                }
            }

            strToSave += "\r\n";
        }

        try
        {
            TextWriter tw = new StreamWriter(newFilePath);
            tw.Write(strToSave);
            tw.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Save settings back to ini file.
    /// </summary>
    public void SaveSettings()
    {
        SaveSettings(iniFilePath);
    }
}
于 2011-02-18T04:08:21.230 に答える
23

joerage's answer のコードは刺激的です。

残念ながら、キーの文字の大文字と小文字が変更され、コメントが処理されません。そのため、非常にダーティな INI ファイル (のみ) を読み取るのに十分な堅牢性があり、キーをそのまま取得できるようにする必要があるものを作成しました。

ネストされた大文字と小文字を区別しない文字列辞書である LINQ を使用して、セクション、キー、および値を格納し、一度にファイルを読み取ります。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class IniReader
{
    Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>(StringComparer.InvariantCultureIgnoreCase);

    public IniReader(string file)
    {
        var txt = File.ReadAllText(file);

        Dictionary<string, string> currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

        ini[""] = currentSection;

        foreach(var line in txt.Split(new[]{"\n"}, StringSplitOptions.RemoveEmptyEntries)
                               .Where(t => !string.IsNullOrWhiteSpace(t))
                               .Select(t => t.Trim()))
        {
            if (line.StartsWith(";"))
                continue;

            if (line.StartsWith("[") && line.EndsWith("]"))
            {
                currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
                ini[line.Substring(1, line.LastIndexOf("]") - 1)] = currentSection;
                continue;
            }

            var idx = line.IndexOf("=");
            if (idx == -1)
                currentSection[line] = "";
            else
                currentSection[line.Substring(0, idx)] = line.Substring(idx + 1);
        }
    }

    public string GetValue(string key)
    {
        return GetValue(key, "", "");
    }

    public string GetValue(string key, string section)
    {
        return GetValue(key, section, "");
    }

    public string GetValue(string key, string section, string @default)
    {
        if (!ini.ContainsKey(section))
            return @default;

        if (!ini[section].ContainsKey(key))
            return @default;

        return ini[section][key];
    }

    public string[] GetKeys(string section)
    {
        if (!ini.ContainsKey(section))
            return new string[0];

        return ini[section].Keys.ToArray();
    }

    public string[] GetSections()
    {
        return ini.Keys.Where(t => t != "").ToArray();
    }
}
于 2013-06-06T21:47:44.987 に答える
4

通常、C# と .NET Framework を使用してアプリケーションを作成する場合、INI ファイルは使用しません。XML ベースの構成ファイルまたはレジストリに設定を保存する方が一般的です。ただし、ソフトウェアがレガシー アプリケーションと設定を共有している場合は、情報を他の場所に複製するよりも、その構成ファイルを使用する方が簡単な場合があります。

.NET フレームワークは、INI ファイルの直接使用をサポートしていません。ただし、Platform Invocation Services (P/Invoke) で Windows API 関数を使用して、ファイルの読み書きを行うことができます。このリンクでは、INI ファイルを表すクラスを作成し、Windows API 関数を使用してそれらを操作します。以下のリンクからお進みください。

INI ファイルの読み書き

于 2012-04-02T06:14:17.747 に答える
2

CommonLibrary.NETで利用できる Ini パーサーがあります。

これには、セクション/値を取得するためのさまざまな非常に便利なオーバーロードがあり、非常に軽量です。

于 2010-01-28T06:27:58.197 に答える
2

これは、正規表現を使用した私自身のバージョンです。このコードは、各セクション名が一意であると想定していますが、そうでない場合は、Dictionary を List に置き換えるのが理にかなっています。この関数は、「;」から始まる .ini ファイルのコメントをサポートしています。キャラクター。セクションは通常 [section] で始まり、キーと値のペアも通常どおり "key = value" になります。セクションと同じ仮定 - キー名は一意です。

/// <summary>
/// Loads .ini file into dictionary.
/// </summary>
public static Dictionary<String, Dictionary<String, String>> loadIni(String file)
{
    Dictionary<String, Dictionary<String, String>> d = new Dictionary<string, Dictionary<string, string>>();

    String ini = File.ReadAllText(file);

    // Remove comments, preserve linefeeds, if end-user needs to count line number.
    ini = Regex.Replace(ini, @"^\s*;.*$", "", RegexOptions.Multiline);

    // Pick up all lines from first section to another section
    foreach (Match m in Regex.Matches(ini, "(^|[\r\n])\\[([^\r\n]*)\\][\r\n]+(.*?)(\\[([^\r\n]*)\\][\r\n]+|$)", RegexOptions.Singleline))
    {
        String sectionName = m.Groups[2].Value;
        Dictionary<String, String> lines = new Dictionary<String, String>();

        // Pick up "key = value" kind of syntax.
        foreach (Match l in Regex.Matches(ini, @"^\s*(.*?)\s*=\s*(.*?)\s*$", RegexOptions.Multiline))
        {
            String key = l.Groups[1].Value;
            String value = l.Groups[2].Value;

            // Open up quotation if any.
            value = Regex.Replace(value, "^\"(.*)\"$", "$1");

            if (!lines.ContainsKey(key))
                lines[key] = value;
        }

        if (!d.ContainsKey(sectionName))
            d[sectionName] = lines;
    }

    return d;
}
于 2016-10-14T20:45:11.777 に答える
-7

オブジェクト全体を xml に保存したり、保存した xml からオブジェクトを作成したりできるため、xml ファイルからデータを読み書きする必要があります。オブジェクトを操作しやすい方がよいでしょう。

オブジェクト データを XML ファイルに書き込む: https://msdn.microsoft.com/en-us/library/ms172873.aspx XML ファイルからオブジェクト データを読み取る: https://msdn.microsoft. com/en-us/library/ms172872.aspx

于 2016-01-13T01:43:28.717 に答える