Can .NET load and parse a properties file equivalent to Java Properties class?

前端 未结 13 1684
死守一世寂寞
死守一世寂寞 2020-12-01 01:19

Is there an easy way in C# to read a properties file that has each property on a separate line followed by an equals sign and the value, such as the following:



        
13条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 01:39

    Final class. Thanks @eXXL.

    public class Properties
    {
        private Dictionary list;
        private String filename;
    
        public Properties(String file)
        {
            reload(file);
        }
    
        public String get(String field, String defValue)
        {
            return (get(field) == null) ? (defValue) : (get(field));
        }
        public String get(String field)
        {
            return (list.ContainsKey(field))?(list[field]):(null);
        }
    
        public void set(String field, Object value)
        {
            if (!list.ContainsKey(field))
                list.Add(field, value.ToString());
            else
                list[field] = value.ToString();
        }
    
        public void Save()
        {
            Save(this.filename);
        }
    
        public void Save(String filename)
        {
            this.filename = filename;
    
            if (!System.IO.File.Exists(filename))
                System.IO.File.Create(filename);
    
            System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
    
            foreach(String prop in list.Keys.ToArray())
                if (!String.IsNullOrWhiteSpace(list[prop]))
                    file.WriteLine(prop + "=" + list[prop]);
    
            file.Close();
        }
    
        public void reload()
        {
            reload(this.filename);
        }
    
        public void reload(String filename)
        {
            this.filename = filename;
            list = new Dictionary();
    
            if (System.IO.File.Exists(filename))
                loadFromFile(filename);
            else
                System.IO.File.Create(filename);
        }
    
        private void loadFromFile(String file)
        {
            foreach (String line in System.IO.File.ReadAllLines(file))
            {
                if ((!String.IsNullOrEmpty(line)) &&
                    (!line.StartsWith(";")) &&
                    (!line.StartsWith("#")) &&
                    (!line.StartsWith("'")) &&
                    (line.Contains('=')))
                {
                    int index = line.IndexOf('=');
                    String key = line.Substring(0, index).Trim();
                    String value = line.Substring(index + 1).Trim();
    
                    if ((value.StartsWith("\"") && value.EndsWith("\"")) ||
                        (value.StartsWith("'") && value.EndsWith("'")))
                    {
                        value = value.Substring(1, value.Length - 2);
                    }
    
                    try
                    {
                        //ignore dublicates
                        list.Add(key, value);
                    }
                    catch { }
                }
            }
        }
    
    
    }
    

    Sample use:

    //load
    Properties config = new Properties(fileConfig);
    //get value whith default value
    com_port.Text = config.get("com_port", "1");
    //set value
    config.set("com_port", com_port.Text);
    //save
    config.Save()
    

提交回复
热议问题