winform 怎么写配置文件?

一休宗纯 2010-08-11 09:26:24
比如由于我要将登录的用户名之类的信息保存,以让用户下次登录不用输入.

总之就是要读写配置文件

我试了 ConfigurationManager 还有 Properties.Settings
好象只能读不能写,请指点!
...全文
84 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
一休宗纯 2010-08-13
  • 打赏
  • 举报
回复
你们的回答都不令人满意,看我的方法:
读: 
textBoxUser.Text = ConfigurationManager.AppSettings["username"];
写:
     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["username"].Value = sUser;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
在 app.config 添加:
<appSettings>
<add key="username" value=""/>
</appSettings>
mayonglong 2010-08-11
  • 打赏
  • 举报
回复

public class ClassConfigurationSettings
{
public ClassConfigurationSettings()
{
}

/// <summary>
/// 修改配置文件(数据库连接字符串)
/// </summary>
/// <param name="connString"></param>
public static void UpdateConfig(string p_strKey, string p_strValue)
{
try
{
string m_strFullPath = "";
Assembly Asm = Assembly.GetExecutingAssembly();
XmlDocument xmlDoc = new XmlDocument();

m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "YourProgramme.exe.config";
xmlDoc.Load(m_strFullPath);

XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;

if (xe.GetAttribute("key").IndexOf(p_strKey) != -1)
{
xe.SetAttribute("value", p_strValue);
}
}
xmlDoc.Save(m_strFullPath);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
}
wangtong2010 2010-08-11
  • 打赏
  • 举报
回复
可以把你的信息写到注册表里。
RockPlus 2010-08-11
  • 打赏
  • 举报
回复
建议自己写重新建一个xml文件保存这些信息或存储到注册表里。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧