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 中,添加一下引用即可。
  • 相关阅读:
    Linux 下编译hello world 的C 语言程序
    C语言实现二维数组操作--元素个数确定
    Linux Eclipse安装和配置命令行(jre、jdk)
    段错误bug的调试
    fopen与open的区别
    同样的c代码,为何在windows下和linux下执行结果不一样?
    VIM快捷键
    浅谈C中的wprintf和宽字符显示
    Know More About Oracle Row Lock
    【教学视频】Maclean教你一步一步使用Vbox在Linux 5上安装Oracle 11gR2 RAC
  • 原文地址:https://www.cnblogs.com/kfx2007/p/3001048.html
Copyright © 2011-2022 走看看