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

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

问题描述

我面临一个问题。

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

i want to save settings in app.config file

i在配置文件中写了单独的类和定义的部分。

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

这里是SettingsClass

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();
        }

这是app.config

and this is app.config

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

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

可以告诉我在哪里是错误..我试图alot和阅读从互联网。但仍然无法保存配置文件中的信息。

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

我通过双击exe文件也运行应用程序。

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

推荐答案

根据 MSDN:ConfigurationManager.GetSection方法

ConfigurationManager.GetSection 方法访问运行时配置信息 。要更改配置,请使用 Configuration.GetSection 方法对您使用以下打开方法之一获得的配置文件:

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:

OpenMachineConfiguration

OpenMappedExeConfiguration

然而,如果你想更新app.config文件,我会把它作为一个xml文件,并把它作为一个普通的xml文件。

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";
        }
    }
}



我还添加了TimeStamp

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天全站免登陆