如题,我知道在项目属性-“设置”中可以添加设置,但是如何在程序中动态添加设置呢?配置文件中对应的是下面这种形式<userSettings>
    <Sample.Properties.Settings>
      <setting name="HH" serializeAs="String">
        <value>ZHIJUN</value>
      </setting>
    </Sample.Properties.Settings>
</userSettings>

解决方案 »

  1.   

    XML,自己写也可以的
    使用XMLDocument
      

  2.   

    要自己读取,用dataset更简单
    DataSet ds = new DataSet();
    ds.ReadXml("c:\\config.xml");
    string param1 = ds.Tables["Table"].Rows[0]["param1"].ToString();
      

  3.   

    楼上几位说的虽然都可以,但是不方便,我是想自己写到App.Config文件中,然后再程序中直接就可以读出来,而不需要用其他方法去打开以个XML文档,现在我已经实现了
      

  4.   

    参考一下
    http://www.cnblogs.com/wangsu/archive/2008/02/25/1081226.html
      

  5.   

    建议不用向App.Config文件中写,App.Config主要是保存整个程序需要的一些设置,也就是一般不随着程序的运行而改变的设置。而User的设置则会随User的不同而不同,建议写一个XML文档,而且这个XML文档不要保存在可执行程序的目录中,比如你的可执行程序在C:\Program files\AAA下,建议这个User配置XML也不要放在这个目录下,原因是因为当有多个Windows用户(比如User1,User2),他们登陆Windows,然后去执行你的程序,那么后一个总会把前一个User配置给覆盖掉,所以建议用户的配置文件放在 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)   目录下。
      

  6.   

    我也不建议写入app.conifg里,姑且不论他是否能写主要是为了隔离设计,我可不希望因为某个意外性的操作,而把app.config这么重要的配置文件给损毁了
      

  7.   

    写XML文档吧,写App.Config文件的话你在配置文件也看不到的,虽然节点值更新了,配置文件还是原来的值
    而且配置文件要重启才生效,如果你刷新的话有些东西会变的
    直接操作XML文件好些,但是XML文件所属文件夹一定要把可读属性勾掉,不然会报错的
    这只是一个建议,如果你坚持修改AppConfig的话可以使用以下函数://AppKey是修改节点的Key,AppValue是传进去的新值
     public static void SetValue(string AppKey, string AppValue)
            {
                XmlDocument xDoc = new XmlDocument();
                //获取可执行文件的路径和名称 
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");            XmlNode xNode;
                XmlElement xElem1;
                XmlElement xElem2;
                xNode = xDoc.SelectSingleNode("//appSettings");//这里可以改成你的节点            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
                else
                {
                    xElem2 = xDoc.CreateElement("add");
                    xElem2.SetAttribute("key", AppKey);
                    xElem2.SetAttribute("value", AppValue);
                    xNode.AppendChild(xElem2);
                }
                xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
            }修改XML的话是这个,下面这个是我用的,你根据情况改一下路径和节点内容 void Modify(String DelayShowingNum)
            {
                try
                {
                    System.IO.DirectoryInfo mDir = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath);
                    System.IO.DirectoryInfo dir = mDir.Parent;
                    System.IO.DirectoryInfo dir_a = dir.Parent;
                    string usr = dir_a.FullName + "\\ProvinceConfig\\Provinces.xml";//得到存省份信息的xml文件
                    XElement xes = XElement.Load(usr);//加载xml文件
                    IEnumerable<XElement> elements = from e in xes.Elements("DisplayPub") select e;
                    foreach (XElement xe in elements)
                    {
                        if (xe.Name == "DisplayPub")
                        {
                            xe.Value = DelayShowingNum;
                        }
                    }
                    xes.Save(usr);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
      

  8.   

    楼上几位你们说的都太复杂了,不用那么麻烦,我自己写了一个类,可以实现在App.Config文件中添加、删除和修改的功能,而且提供了加密的功能。/// <summary>
            /// 获取一个值,改值指示配置文件的AppSettings配置节中是否含有和参数“key”同名的键
            /// </summary>
            /// <param name="key">要判断的key</param>
            /// <returns></returns>
            public static bool HasAppSettingsKey(string key)
            {
                bool hasKey = false;            for (int i = 0; i < ConfigurationManager.AppSettings.Keys.Count; i++)
                {
                    if (ConfigurationManager.AppSettings.Keys[i] == key)
                    {
                        hasKey = true;
                    }
                }            return hasKey;
            }        /// <summary>
            /// 给appSettings节添加元素(键值对)
            /// </summary>
            /// <param name="key">键</param>
            /// <param name="value">值</param>
            /// <param name="ex">out类型参数:存储错误信息</param>
            /// <returns></returns>
            public static bool AddAppSettings(string key, string value, out string ex)
            {
                try
                {
                    if (HasAppSettingsKey(key))
                    {                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                        config.AppSettings.Settings[key].Value = value;
                        config.AppSettings.SectionInformation.ForceSave = true;
                        config.Save();
                        ConfigurationManager.RefreshSection("appSettings");                    ex = "更新成功";
                    }
                    else
                    {
                        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                        config.AppSettings.Settings.Add(key, value);
                        config.AppSettings.SectionInformation.ForceSave = true;
                        config.Save();
                        ConfigurationManager.RefreshSection("appSettings");
                        //config.AppSettings.Settings[key].                    ex = "添加成功";
                    }                return true;
                }
                catch
                {
                    ex = "添加失败";
                    return false;
                }
            }        /// <summary>
            /// 加密appSettings配置节
            /// </summary>
            /// <param name="ex">out类型参数:存储错误信息</param>
            /// <returns></returns>
            public static bool EncryptAppSettings(out string ex)
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.AppSettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                    config.AppSettings.SectionInformation.ForceSave = true;
                    config.Save();
                    ex = "加密成功";                return true;
                }
                catch (Exception e)
                {
                    ex = e.Message;
                    return false;
                }
            }        /// <summary>
            /// 加密connectionStrings配置节
            /// </summary>
            /// <param name="ex">out类型参数:存储错误信息</param>
            /// <returns></returns>
            public static bool EncryptConnectionStrings(out string ex)
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.ConnectionStrings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                    config.ConnectionStrings.SectionInformation.ForceSave = true;
                    config.Save();
                    ex = "加密成功";                return true;
                }
                catch(Exception e)
                {
                    ex = e.Message;
                    return false;
                }
            }