Simplest possible key/value pair file parsing in .NET

前端 未结 5 1721
闹比i
闹比i 2021-01-17 19:07

My project requires a file where I will store key/value pair data that should be able to be read and modified by the user. I want the program to just expect the keys to be t

相关标签:
5条回答
  • 2021-01-17 19:17

    I don't know of any builtin class to parse ini file. I've used nini when needed to do so. It's licensed under the MIT/X11 license, so doesn't have any issue to be included in a closed source program.

    It's very to use. So if you have a Settings.ini file formatted this way:

    [Configuration]
    Name = Jb Evain
    Phone = +330101010101
    

    Using it would be as simple as:

    var source = new IniConfigSource ("Settings.ini");
    var config = source.Configs ["Configuration"];
    
    string name = config.Get ("Name");
    string phone = config.Get ("Phone");
    
    0 讨论(0)
  • 2021-01-17 19:20

    If you're looking for a quick easy function and don't want to use .Net app\user config setting files or worry about serialization issues that sometimes occur of time.

    The following static function can load a file formatted like KEY=VALUE.

    public static Dictionary<string, string> LoadConfig(string settingfile)
    {
        var dic = new Dictionary<string, string>();
    
        if (File.Exists(settingfile))
        {
            var settingdata = File.ReadAllLines(settingfile);
            for (var i = 0; i < settingdata.Length; i++)
            {
                var setting = settingdata[i];
                var sidx = setting.IndexOf("=");
                if (sidx >= 0)
                {
                    var skey = setting.Substring(0, sidx);
                    var svalue = setting.Substring(sidx+1);
                    if (!dic.ContainsKey(skey))
                    {
                        dic.Add(skey, svalue);
                    }
                }
            }
        }
    
        return dic;
    }
    

    Note: I'm using a Dictionary so keys must be unique, which is usually that case with setting.

    USAGE:

    var settingfile = AssemblyDirectory + "\\mycustom.setting";
    var settingdata = LoadConfig(settingfile);
    if (settingdata.ContainsKey("lastrundate"))
    {
        DateTime lout;
        string svalue;
        if (settingdata.TryGetValue("lastrundate", out svalue))
        {
            DateTime.TryParse(svalue, out lout);
            lastrun = lout;
        }
    }
    
    0 讨论(0)
  • 2021-01-17 19:32

    Format the file this way:

    key1=value1
    key2=value2
    

    Read the entire file into a string (there is a simple convenience function that does that, maybe in the File or string class), and call string.Split('='). Make sure you also call string.Trim() on each key and value as you traverse the list and pop each pair into a hashtable or dictionary.

    0 讨论(0)
  • 2021-01-17 19:34

    Use the KeyValuePair class for you Key and Value, then just serialize a List to disk with an XMLSerializer.

    That would be the simplest approach I feel. You wouldn't have to worry about traversing nodes. Calling the Deserialize function will do that for you. The user could edit the values in the file if they wish also.

    0 讨论(0)
  • 2021-01-17 19:36

    if you want the user to be able to read and modify the file, i suggest a comma-delimited pair, one per line

    key1,value1
    key2,value2
    ...
    

    parsing is simple: read the file, split at newline or comma, then take the elements in pairs

    0 讨论(0)
提交回复
热议问题