exePath must be specified when not running inside a stand alone exe

A library own package, wanted to facilitate their re-use code is as follows:


		/// <summary>
        /// 写入配置文件的值
        /// </summary>
        /// <param name="key">key键</param>
        /// <param name="value">value值</param>
        /// <returns>写入成功返回true,否则返回false,有异常</returns>
        public static bool Write(string key, string value)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[key].Value = value;

                config.AppSettings.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);


                //debug模式中不会更改实际文件中的内容,release后更改
                ConfigurationManager.RefreshSection("appSettings");

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

The effect of the method is to store a value in the configuration file AppSetting node. But I do not want the title today met when using the Web site in question.

The solution is to rewrite a method for the web site:

/// <summary>
        /// 写入Web配置文件的值
        /// </summary>
        /// <param name="key">key键</param>
        /// <param name="value">value值</param>
        /// <returns>写入成功返回true,否则返回false,有异常</returns>
        public static bool WriteWebConfig(string key, string value)
        {
            try
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
                config.AppSettings.Settings[key].Value = value;

                config.AppSettings.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);


                //debug模式中不会更改实际文件中的内容,release后更改
                ConfigurationManager.RefreshSection("appSettings");

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

WebConfigurationManager System.Web.dll class, add a reference to it.

Published 51 original articles · won praise 11 · views 6091

Guess you like

Origin blog.csdn.net/weixin_42140261/article/details/102607727