无法在 app.exe.config 中保存设置 [英] unable to save settings in app.exe.config

查看:24
本文介绍了无法在 app.exe.config 中保存设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个问题.

我想在 app.config 文件中保存设置

我在配置文件中编写了单独的类和定义的部分..

但是当我运行应用程序时.它不会将给定的值保存到配置文件中

这里是设置类

公共类 MySetting:ConfigurationSection{private static MySetting settings = ConfigurationManager.GetSection("MySetting") as MySetting;公共覆盖 bool IsReadOnly(){返回假;}公共静态 MySetting 设置{得到{返回设置;}}[ConfigurationProperty("CustomerName")]公共字符串客户名称{得到{返回设置["CustomerName"].ToString();}放{设置[客户名称"] = 值;}}[ConfigurationProperty("EmailAddress")]公共字符串电子邮件地址{得到{返回设置["电子邮件地址"].ToString();}放{设置[电子邮件地址"] = 值;}}公共静态布尔保存(){尝试{System.Configuration.Configuration configFile = Utility.GetConfigFile();MySetting mySetting = (MySetting )configFile.Sections["MySetting "];if (null != mySetting ){mySetting .CustomerName = settings["CustomerName"] 作为字符串;mySetting .EmailAddress = settings["EmailAddress"] 作为字符串;configFile.Save(ConfigurationSaveMode.Full);返回真;}返回假;}抓住{返回假;}}}

这是我在配置文件中保存信息的代码

private void SaveCustomerInfoToConfig(String name, String emailAddress){MySetting .Settings.CustomerName = name;MySetting .Settings.EmailAddress = emailAddress我的设置 .Save();}

这是 app.config

<预><代码><配置><configSections><section name="MySettings" type="TestApp.MySettings, TestApp"/></configSections><MySettings CustomerName=""EmailAddress=""/></配置>

你能告诉我错误在哪里..我尝试了很多并从互联网上阅读.但仍然无法在配置文件中保存信息..

我也通过双击 exe 文件运行了该应用程序.

解决方案

根据 MSDN:ConfigurationManager.GetSection 方法

ConfigurationManager.GetSection 方法访问运行时配置信息它不能改变.要更改配置,请使用 Configuration.GetSection 方法,您可以使用以下 Open 方法之一获取配置文件:

但是,如果您想更新 app.config 文件,我会将其作为 xml 文档读取并作为普通 xml 文档进行操作.

请看下面的例子:注意:此示例仅用于概念验证.不应按原样用于生产.

使用系统;使用 System.Linq;使用 System.Xml.Linq;命名空间 ChangeAppConfig{课程计划{static void Main(string[] args){MyConfigSetting.CustomerName = "MyCustomer";MyConfigSetting.EmailAddress = "MyCustomer@Company.com";MyConfigSetting.TimeStamp = DateTime.Now;MyConfigSetting.Save();}}//注意:这是一个概念验证示例和//不应该按原样用于生产.//例如,这不是线程安全的.公共类 MyConfigSetting{私有静态字符串 _CustomerName;公共静态字符串 CustomerName{得到 { 返回 _CustomerName;}放{_CustomerName = 值;}}私有静态字符串_EmailAddress;公共静态字符串电子邮件地址{得到 { 返回 _EmailAddress;}放{_EmailAddress = 值;}}私有静态 DateTime _TimeStamp;公共静态日期时间时间戳{得到 { 返回 _TimeStamp;}放{_TimeStamp = 值;}}公共静态无效保存(){XElement myAppConfigFile = XElement.Load(Utility.GetConfigFileName());var mySetting = (来自 myAppConfigFile.Elements("MySettings") 中的 p选择 p).FirstOrDefault();mySetting.Attribute("CustomerName").Value = CustomerName;mySetting.Attribute("EmailAddress").Value = EmailAddress;mySetting.Attribute("TimeStamp").Value = TimeStamp.ToString();myAppConfigFile.Save(Utility.GetConfigFileName());}}类实用程序{//注意:这是一个概念验证且非常幼稚的代码.//不应按原样用于生产.//例如,没有空引用检查,没有文件存在检查等.公共静态字符串 GetConfigFileName(){const string STR_Vshostexe = ".vshost.exe";string appName = Environment.GetCommandLineArgs()[0];//如果这是在调试器下运行.if (appName.EndsWith(STR_Vshostexe)){appName = appName.Remove(appName.LastIndexOf(STR_Vshostexe), STR_Vshostexe.Length) + ".exe";}返回 appName + ".config";}}}

我还在 app.config 中的 MySettings 中添加了TimeStamp"属性,以便轻松检查结果.

</配置>

i am facing one problem.

i want to save settings in app.config file

i wrote separate class and defined section in config file..

but when i run the application. it does not save the given values into config file

here is SettingsClass

public class MySetting:ConfigurationSection
    {
        private static MySetting settings = ConfigurationManager.GetSection("MySetting") as MySetting;

        public override bool IsReadOnly()
        {
            return false;
        }

        public static MySetting Settings
        {
            get
            {
                return settings;
            }
        }


        [ConfigurationProperty("CustomerName")]
        public String CustomerName
        {
            get
            {
                return settings["CustomerName"].ToString();
            }
            set
            {
                settings["CustomerName"] = value;
            }
        }


        [ConfigurationProperty("EmailAddress")]
        public String EmailAddress
        {
            get
            {                
                return settings["EmailAddress"].ToString();
            }
            set
            {
                settings["EmailAddress"] = value;
            }
        }


        public static bool Save()
        {
            try
            {
                System.Configuration.Configuration configFile = Utility.GetConfigFile();
                MySetting mySetting = (MySetting )configFile.Sections["MySetting "];

                if (null != mySetting )
                {
                    mySetting .CustomerName = settings["CustomerName"] as string;
                    mySetting .EmailAddress = settings["EmailAddress"] as string;                    

                    configFile.Save(ConfigurationSaveMode.Full);
                    return true;
                }

                return false;
            }
            catch
            {
                return false;
            }
        }
    }

and this is the code from where i am saving the information in config file

private void SaveCustomerInfoToConfig(String name, String emailAddress)
        {            

            MySetting .Settings.CustomerName = name;
            MySetting .Settings.EmailAddress = emailAddress
            MySetting .Save();
        }

and this is app.config

<configuration>
  <configSections>
    <section name="MySettings" type="TestApp.MySettings, TestApp"/>
  </configSections> 

  <MySettings CustomerName="" EmailAddress="" />  
</configuration>

can u tell me where is the error.. i tried alot and read from internet. but still unable to save information in config file..

i ran the application by double clicking on exe file also.

解决方案

According to the MSDN: ConfigurationManager.GetSection Method,

The ConfigurationManager.GetSection method accesses run-time configuration information that it cannot change. To change the configuration, you use the Configuration.GetSection method on the configuration file that you obtain by using one of the following Open methods:

However, if you want to update app.config file, I would read it as an xml document and manipulate it as a normal xml document.

Please see the following example: Note: this sample is just for proof-of-concept. Should not be used in production as it is.

using System;
using System.Linq;
using System.Xml.Linq;

namespace ChangeAppConfig
{
    class Program
    {
        static void Main(string[] args)
        {
            MyConfigSetting.CustomerName = "MyCustomer";
            MyConfigSetting.EmailAddress = "MyCustomer@Company.com";
            MyConfigSetting.TimeStamp = DateTime.Now;
            MyConfigSetting.Save();
        }
    }

    //Note: This is a proof-of-concept sample and 
    //should not be used in production as it is.  
    // For example, this is not thread-safe. 
    public class MyConfigSetting
    {
        private static string _CustomerName;
        public static string CustomerName
        {
            get { return _CustomerName; }
            set
            {
                _CustomerName = value;
            }
        }

        private static string _EmailAddress;
        public static string EmailAddress
        {
            get { return _EmailAddress; }
            set
            {
                _EmailAddress = value;
            }
        }

        private static DateTime _TimeStamp;
        public static DateTime TimeStamp
        {
            get { return _TimeStamp; }
            set
            {
                _TimeStamp = value;
            }
        }

        public static void Save()
        {
            XElement myAppConfigFile = XElement.Load(Utility.GetConfigFileName());
            var mySetting = (from p in myAppConfigFile.Elements("MySettings")
                            select p).FirstOrDefault();
            mySetting.Attribute("CustomerName").Value = CustomerName;
            mySetting.Attribute("EmailAddress").Value = EmailAddress;
            mySetting.Attribute("TimeStamp").Value = TimeStamp.ToString();

            myAppConfigFile.Save(Utility.GetConfigFileName());

        }
    }

    class Utility
    {        
        //Note: This is a proof-of-concept and very naive code. 
        //Shouldn't be used in production as it is. 
        //For example, no null reference checking, no file existence checking and etc. 
        public static string GetConfigFileName()
        {            
            const string STR_Vshostexe = ".vshost.exe";
            string appName = Environment.GetCommandLineArgs()[0];

            //In case this is running under debugger. 
            if (appName.EndsWith(STR_Vshostexe))
            {
                appName = appName.Remove(appName.LastIndexOf(STR_Vshostexe), STR_Vshostexe.Length) + ".exe";
            }

            return appName + ".config";
        }
    }
}

I also added "TimeStamp" attribute to MySettings in app.config to check the result easily.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MySettings" type="TestApp.MySettings, TestApp"/>
  </configSections>

  <MySettings CustomerName="" EmailAddress="" TimeStamp=""/>
</configuration> 

这篇关于无法在 app.exe.config 中保存设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆