twzheng's cppblog

『站在风口浪尖紧握住鼠标旋转!』 http://www.cnblogs.com/twzheng

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  136 随笔 :: 78 文章 :: 353 评论 :: 0 Trackbacks
[问]VS2005,C#winform程序,代码修改app.config的结果保存到哪里了?

通过Properties.Settings用代码形式读写app.config文件,其中Properties.Settings变量的范围"scope"都设置为用户"user"(注:如果改为"Application",编译时系统提示其为只读属性),读写都正常,并且重新打开exe文件时,上次输入的值仍然存在,但是手动打开"test.exe.config",所有的设置变量值均为空,写入的值都保存到什么地方去了呢?临时文件?我用360安全卫士清除临时文件后在此打开exe文件,上次输入的值依然存在。有谁知道这是怎么回事?

示例代码:
private void button1_Click(object sender, EventArgs e)

   //读操作,将读到的值送textBox1显示 
   Properties.Settings config = Properties.Settings.Default; 
   textBox1.Text = config.myvar;
}

private void button2_Click(object sender, EventArgs e)

   //写操作,将textBox2里的值写入myvar 
   Properties.Settings config = Properties.Settings.Default; 
   config.myvar = textBox2.Text; 
   config.Save();
}
posted on 2008-03-17 21:32 谭文政 阅读(3444) 评论(4)  编辑 收藏 引用 所属分类: windows 编程

评论

# re: [问]VS2005,C#winform程序,代码修改app.config的结果保存到哪里了?[未登录] 2008-03-18 08:24 cppexplore
@苦味酸
c#的问题建议去博客园问,这边看样子知道的不多。另有留言给你。  回复  更多评论
  

# re: [问]VS2005,C#winform程序,代码修改app.config的结果保存到哪里了? 2008-03-18 09:06 苦味酸
@cppexplore
谢谢  回复  更多评论
  

# re: [问]VS2005,C#winform程序,代码修改app.config的结果保存到哪里了? 2008-03-18 10:55 苦味酸
自己找到问题答案了。。。

当Properties.Settings变量的范围"scope"设置为用户"user"时,通过上述方式读写操作并不是操作了"test.exe.config"文件,实际操作的文件保存在"C:\Documents and Settings\Administrator\Local Settings\Application Data\"路径下面(注:Administrator是当前用户文件夹),文件名字叫"user.config"。点击工程Properties页面中"设置"选项卡的"同步"按钮会提示这个路径。  回复  更多评论
  

# re: [问]VS2005,C#winform程序,代码修改app.config的结果保存到哪里了? 2008-03-18 11:05 苦味酸
用下面的方法可以操作应用程序文件夹下的配置文件:

在winform中使用程序读取和修改App.config里面的appSettings当中的Value值

这里我写成了两个方法,以供大家参考!
一,命名空间
using System;
using System.Configuration;
using System.Xml;
二,方法
//读取Value值
public static string GetConfigString(string key)
{
//
// TODO: 在此处添加构造函数逻辑
//
return ConfigurationSettings.AppSettings[key];
}
//写操作
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");
}
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理