winform读取配置文件App.config

有时候一些用户的配置需要写入在本地,不能每次程序启动都让用户重新设置一下吧。

下面先说基本用法:

1、创建winform项目之后自动会生成App.config文件,如果默认没有就对项目右键-新建项-配置文件

这个文件创建后自动就有的,如果没有就新建一个,我们可以往里面添加需要的数据:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
      <add key="key1" value="hello"/>
      <add key="key2" value="world!"/>
    </appSettings>
</configuration>

里面的appSettings节点和子节点就是我们添加上去的

2、读写

项目的引用增加System.Configuration,然后在cs文件引入System.Configuration

然后这样就可以读到数据了:

            string key1 = ConfigurationManager.AppSettings["key1"];
            Debug.WriteLine("key1=" + key1);

这样修改数据:

            ConfigurationManager.AppSettings.Set("key1","new hello!");
            Debug.WriteLine("new-key1=" + ConfigurationManager.AppSettings["key1"]);
一切看起来都非常简单,没有什么问题,打印出来显示都是正确的。

实际上他设置的时候并没有真正写入xml,而是修改了内存中的值而已!


3、正确的做法应该是按照对待一个普通的xml来进行读写操作,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace TestConfig
{
    class ConfigSettings
    {
        private ConfigSettings() { }
        public static string ReadSetting(string key) 
        {
            //不建议通过这种自带的方式进行读取;如果手动修改了配置文件,则不会第二次读取的时候,依旧是内存中的值。可以通过XML方式进行读取。
            //return ConfigurationSettings.AppSettings[key];
            XmlDocument doc = loadConfigDocument();

            XmlNode node = doc.SelectSingleNode("//appSettings");

            if (node == null) 
                throw new InvalidOperationException("appSettings section not found in config file.");

            try
            {
                XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));

                if (elem != null) 
                {
                    return elem.GetAttribute("value");
                }
            }
            catch 
            {
                throw;
            }

            return "";
        }

        public static void WriteSetting(string key, string value) 
        {
            XmlDocument doc = loadConfigDocument();

            XmlNode node = doc.SelectSingleNode("//appSettings");

            if (node == null)
                throw new InvalidOperationException("appSettings section not found in config file.");

            try
            {
                XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));

                if (elem != null)
                {
                    elem.SetAttribute("value", value);
                }
                else
                {
                    elem = doc.CreateElement("add");
                    elem.SetAttribute("key", key);
                    elem.SetAttribute("value", value);
                    node.AppendChild(elem);
                }
                doc.Save(getConfigFilePath());
            }
            catch 
            {
                throw;
            }
        }

        public static void RemoveSetting(string key) 
        {
            XmlDocument doc = loadConfigDocument();

            XmlNode node = doc.SelectSingleNode("//appSettings");

            try
            {
                if (node == null)
                    throw new InvalidOperationException("appSettings section not found in config file.");
                else
                {
                    node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
                    doc.Save(getConfigFilePath());
                }
            }
            catch(NullReferenceException e) 
            {
                throw new Exception(string.Format("The key {0} does not exist.", key), e);
            }
        }

        private static XmlDocument loadConfigDocument()
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load(getConfigFilePath());
                return doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("No configuration file found.", e);
            }
        }

        private static string getConfigFilePath()
        {
            return Assembly.GetExecutingAssembly().Location + ".config";
        }
    }
}



参考文章:https://www.cnblogs.com/zfanlong1314/p/3623622.html

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WinForm读取Oracle配置文件,可以按照以下步骤进行: 1. 首先,确保已经正确安装了Oracle客户端,并配置好了环境变量。这样才能在WinForm中正确访问Oracle数据库。 2. 在WinForm的项目中添加一个配置文件,可以命名为“App.config”,这个文件将用于存储Oracle数据库的连接信息。 3. 在配置文件中添加以下代码,用于配置Oracle数据库的连接信息: ```xml <configuration> <appSettings> <add key="OracleConnectionString" value="Data Source=数据库地址;User ID=用户名;Password=密码;"/> </appSettings> </configuration> ``` 其中,将“数据库地址”替换为实际的Oracle数据库地址、“用户名”替换为要连接的数据库的用户名、“密码”替换为对应的密码。 4. 在WinForm中的代码中,可以通过以下方法读取配置文件中的连接信息: ```csharp string connectionString = ConfigurationManager.AppSettings["OracleConnectionString"]; ``` 这样就可以获取到配置文件中存储的Oracle数据库的连接字符串。 5. 使用获取到的连接字符串进行数据库操作,例如连接数据库、执行SQL语句等。 需要注意的是,配置文件中的连接信息可以根据实际情况进行修改,以适应不同的Oracle数据库连接需求。另外,在代码中访问配置文件需要引用`System.Configuration`命名空间。 以上就是在WinForm读取Oracle配置文件的基本步骤。通过配置文件,在不同环境下可以方便地修改连接信息,使得程序更加灵活和易于维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值