C#读取配置文件源代码

来源:互联网 发布:python sys.exit 参数 编辑:程序博客网 时间:2024/04/29 13:23

example1:mysql.ini

[mysql]
username=root;

//注释
password=123456

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql

///////////////////////////////////////////////////////////////////////////////////////////////////////////

FileAssistant.cs

//////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileAssistant
{
    public class Assistant
    {
        private String path,section="section1";
        private StreamWriter writer=null;
        private StreamReader reader=null;
        private bool isSection = false,isReader=false,isAppend;
        public String Path
        {
            set
            {
                path = value;
            }
            get
            {
                return path;
            }
        }
        public String Section
        {
            set
            {
                section = value;
            }
            get
            {
                return section;
            }
        }
            public Assistant()
            {
            }
            public void parse(String path, bool isAppend)
             {
                            this.path = path;
                            this.isAppend = isAppend;
              }
            public void put(String Key, String Value)
            {
                if (isAppend)
                {
                    writer = File.AppendText(path);
                }
                else
                {
                    writer = File.CreateText(path);
                }
                if (!isSection)
                {
                    writer.WriteLine("["+section+"]");
                }
                writer.WriteLine(Key+"="+Value);
                writer.Flush();
                writer.Close();
                isSection = true;
            }
            public String get(String Key)
            {
                if (File.Exists(path))
                {
                    try
                    {
                        String line = "";
                        String Value = "";
                        reader = File.OpenText(path);
                        line = reader.ReadLine();
                        while (line != null)
                        {
                            if (line.IndexOf("[" + section + "]") >= 0)
                            {
                                continue;
                            }
                            else
                                if (line.IndexOf(";") >= 0 || line.IndexOf("#") >= 0)
                                {
                                    continue;
                                }
                                else
                                    if (line.IndexOf(Key) >= 0 && line.IndexOf(";") < 0 && line.IndexOf("#") < 0)
                                    {
                                        Value = line.Substring(Key.Length + "=".Length);
                                    }
                            line = reader.ReadLine();
                        }

                    }
                    catch (Exception e)
                    {
                        reader.Close();
                        throw new Exception(e.Message);
                    }
                }
                else
                {
                    throw new FileNotFoundException(path+"未找到,请确认文件是否存在!");
                }
                reader.Close();
                return Value;

            }
            public void release()
            {
                        if (isSection)
                        {
                            writer.Close();
                        }
                        else if(isReader)
                        {
                            reader.Close();
                        }
                        reader = null;
                        writer = null;
            }
            ~Assistant()
            {
                    if (isSection)
                    {
                        writer.Close();
                    }
                    else if (isReader)
                    {
                        reader.Close();
                    }
                    reader = null;
                    writer = null;
            }

    }
}

///////////////////////////////////////////////////////////////////

Test.cs

///////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileAssistant;
namespace AssistantTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Assistant file = new Assistant();
            file.parse("c://mysql.ini",true);
            /**file.Section = "mysql";
            file.put("username","root");
            file.put("password", "123456");
            file.put("driver", "com.mysql.jdbc.Driver");
            file.put("url", "jdbc:mysql://localhost:3306/mysql");*/
            Console.WriteLine(file.get("username"));
            Console.WriteLine(file.get("password"));
            Console.WriteLine(file.get("driver"));
            Console.WriteLine(file.get("url"));
            file.release();
            Console.ReadLine();
        }
    }
}

result:

         root
        123456

      

 

         com.mysql.jdbc.Driver
         jdbc:mysql://localhost:3306/mysql

原创粉丝点击