zoukankan      html  css  js  c++  java
  • exePath must be specified when not running inside a stand alone exe Kevin

    自己封装了一个类库,本来是想方便自己重复使用的,代码如下:

     1 /// <summary>
     2         /// 写入配置文件的值
     3         /// </summary>
     4         /// <param name="key">key键</param>
     5         /// <param name="value">value值</param>
     6         /// <returns>写入成功返回true,否则返回false,有异常</returns>
     7         public static bool Write(string key, string value)
     8         {
     9             try
    10             {
    11                 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    12                 config.AppSettings.Settings[key].Value = value;
    13 
    14                 config.AppSettings.SectionInformation.ForceSave = true;
    15                 config.Save(ConfigurationSaveMode.Modified);
    16 
    17 
    18                 //debug模式中不会更改实际文件中的内容,release后更改
    19                 ConfigurationManager.RefreshSection("appSettings");
    20 
    21                 return true;
    22             }
    23             catch (Exception ex)
    24             {
    25                 return false;
    26             }
    27         }

    该方法的作用就是将值保存到配置文件中的AppSetting节点中。但不想今天在Web网站中使用时碰到了标题的问题。

    解决的方法是重写了一个针对web网站的方法:

    /// <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 中,添加一下引用即可。
  • 相关阅读:
    Android Fragment与Activity通讯详解
    Fragment之间的交互
    理解Fragment的生命周期
    Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
    【APP UI 设计模式】(一)APP UI 设计原则和流程
    白话经典算法系列之六 高速排序 高速搞定
    究竟什么是推荐?
    Mongo散记--聚合(aggregation)&amp; 查询(Query)
    C/C++程序猿必须熟练应用的开源项目
    PL/SQL连接oracle数据库
  • 原文地址:https://www.cnblogs.com/kfx2007/p/3001048.html
Copyright © 2011-2022 走看看