基于.NET5的winform获取配置文件

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="ConnectionString" value="server=.;database=Test;uid=sa;pwd=123123" />
        <add key="AccDB" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Test.mdb;Persist Security Info=True" />
        <!--软件名称-->
        <add key="ApplicationName" value="" />
        <!--开发商名称-->
        <add key="Manufacturer" value="" />
        <!--是否自动更新-->
        <add key="AutoUpdate" value="False" />
        <!--初始化操作-->
        <add key="ClientSettingsProvider.ServiceUri" value="" />
    </appSettings>
</configuration>

 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Sunny.Net.MyForms.Core
{
    public class AppConfig
    {

        private string filePath;

        public string AppName => AppConfigGet("ApplicationName");

        public string Manufacturer => AppConfigGet("Manufacturer");
        public AppConfig()
        {
            //开发环境下与活动的配置文件路径不同
            string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath; //Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App.Config");

            if (File.Exists(path))
            {
                filePath = path;
                return;
            } 
            throw new ArgumentNullException("没有找到Web.Config文件或者应用程序配置文件, 请指定配置文件");
        }

        public AppConfig(string configFilePath)
        {
            filePath = configFilePath;
        }

        public void AppConfigSet(string keyName, string keyValue)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filePath);
            XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("add");
            for (int i = 0; i < elementsByTagName.Count; i++)
            {
                XmlAttribute xmlAttribute = elementsByTagName[i].Attributes["key"];
                if (xmlAttribute != null && xmlAttribute.Value == keyName)
                {
                    xmlAttribute = elementsByTagName[i].Attributes["value"];
                    if (xmlAttribute != null)
                    {
                        xmlAttribute.Value = keyValue;
                        break;
                    }
                }
            }

            xmlDocument.Save(filePath);
        }

        public string AppConfigGet(string keyName)
        {
            string result = string.Empty;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(filePath);
                XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("add");
                for (int i = 0; i < elementsByTagName.Count; i++)
                {
                    XmlAttribute xmlAttribute = elementsByTagName[i].Attributes["key"];
                    if (xmlAttribute != null && xmlAttribute.Value == keyName)
                    {
                        xmlAttribute = elementsByTagName[i].Attributes["value"];
                        if (xmlAttribute != null)
                        {
                            result = xmlAttribute.Value;
                            break;
                        }
                    }
                }
            }
            catch
            {
            }

            return result;
        }

        public string GetSubValue(string keyName, string subKeyName)
        {
            string text = AppConfigGet(keyName).ToLower();
            string[] array = text.Split(';');
            for (int i = 0; i < array.Length; i++)
            {
                string text2 = array[i].ToLower();
                if (text2.IndexOf(subKeyName.ToLower()) >= 0)
                {
                    int num = array[i].IndexOf("=");
                    return array[i].Substring(num + 1);
                }
            }

            return string.Empty;
        }

    }
}

 



posted @ 2022-08-18 09:16  稻花香里说丰年&#127800;  阅读(258)  评论(0编辑  收藏  举报