C# read and write Properties configuration file

C# read and write Properties file

I wanted to read and write some configuration data in the project. After some exploration, the INI file needs to reference the API; the XML hierarchy is unnecessary and troublesome.

At this time, it is very convenient to think of Java's Properties configuration file, the format is similar to INI, write one by yourself.

 

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

/*
 * @author : RoadToTheExpert
 * @date : 2019.07
 * @file : Properties.cs
 */
namespace roadtotheexpert
{
    public class Properties
    {
        public static Hashtable Load(string file)
        {
            Hashtable ht = new Hashtable(16);
            string content = null;
            try
            {
                content = File.ReadAllText(file, System.Text.Encoding.UTF8);
            }
            catch (Exception e)
            {
                return null;
            }
            string[] rows = content.Split('\n');
            string[] kv = null;
            foreach (string c in rows)
            {
                if (c.Trim().Length == 0)
                    continue;
                kv = c.Split('=');
                if (kv.Length == 1)
                {
                    ht[kv[0].Trim()] = "";
                }
                else if (kv.Length == 2)
                {
                    ht[kv[0].Trim()] = kv[1].Trim();
                }
            }
            return ht;
        }

        public static bool Save(string file, Hashtable ht)
        {
            if (ht == null || ht.Count == 0)
                return false;
            StringBuilder sb = new StringBuilder(ht.Count * 12);
            foreach (string k in ht.Keys)
            {
                sb.Append(k).Append('=').Append(ht[k]).Append(System.Environment.NewLine);
            }
            try
            {
                File.WriteAllText(file, sb.ToString(), System.Text.Encoding.UTF8);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static string test()
        {
            string file = @"D:\cfg.txt";
            if (!File.Exists(file)) 
                File.WriteAllText(file, "");

            Hashtable ht = Properties.Load(file);
            if (ht != null)
            {
                ht["Time" + ht.Count] = System.DateTime.Now.ToString();
                Properties.Save(file, ht);

                StringBuilder sb = new StringBuilder(ht.Count * 12);
                foreach (string k in ht.Keys)
                {
                    sb.Append(k).Append('=').Append(ht[k]).Append(System.Environment.NewLine);
                }
                return sb.ToString();
            }
            return "none";
        }

        public static void Main(string[] args)
        {
            Console.WriteLine(test());
        }

        
    }
}

 

Guess you like

Origin blog.csdn.net/RoadToTheExpert/article/details/95964809