我想给网站做一个配置工具,请问Winform如何读取Web。config中appSettings中的键值集合呢?
解决方案 »
- vs2010用的3.5框架,引用事物using System.Transactions;提示缺少程序集
- c#替换字符串中的某些字符
- 如何取excel中其中一个表的数据
- 增加字段绑定在dataGrid上,自动增加值为null????
- c#如何通过反射实现指定类名的实例
- 请教各位网友“鎮ㄥソ锛”是什么意思啊,谢谢!
- 怎么样才能在datagrid上单击,
- 求注释程序!!!最好一段一注释!!!,不行一行也行!!!!
- 如何在ShowInTaskbar属性为false时使用Process.CloseMainWindow()结束该进程
- WPF中文本框获得焦点时打开外部程序后失去焦点,如何重新获取焦点
- 提取网页XML内容
- 如果要画一个坐标系显示曲线,应该用什么控件啊,或者在什么控件上画
txdid=ConfigurationSettings.AppSettings["DataSJXHPath"]; //适用WINFORM
WebConfigurationManager.AppSettings["key"];
not web
ConfigurationManager.AppSettings["key"];
{
System.Xml.XmlDocument webconfig = new System.Xml.XmlDocument();
webconfig.Load(@"F:\CcharpExample\WebSite59\Web.config");//web.config的路径
System.Xml.XmlNode node = webconfig.SelectSingleNode("/configuration/appSettings/add[@key='123']");//要找的节点 if (node == null)
{
MessageBox.Show("没有找到符合要求的节点");
}
else
{
MessageBox.Show(node.Attributes["value"].InnerText);//value的值
}
}在本地帮你测试一下,改一下路径,换一下key值
2.如果你是3.5以上也可以用XML.Linq去读取
不行的,我读的不是应用程序的配置文件,我上面说了,是网站的Web.config...
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
using System.Xml;
namespace DSZQ
{
public partial class DataConn : Form
{
public DataConn()
{
InitializeComponent();
//filepath = Environment.CurrentDirectory;
filepath =Application.StartupPath;
filepath += "//App.config";
}
string filepath;
string connstr;
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="strValue"></param>
public void Modify(string key, string strValue) //两个参数:要修改的键值 和 要修改的新值;
{
string flagstr = strValue;
if (strValue == string.Empty)
{
MessageBox.Show("连接串不能为空!");
return;
}
//string XPath = "/configuration/userInfo/add[@key='?']";
try
{
string XPath = "/configuration/appSettings/add[@key='?']";
XmlDocument domWebConfig = new XmlDocument(); //domWebConfig.Load((HttpContext.Current.Server.MapPath("web.config")));
domWebConfig.Load(filepath);
XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key))); if (addKey == null)
{
//Response.Write("<script>alert (\"没有找到<add key='" + key + "' value=.../>的配置节\")</script>");
MessageBox.Show("没有找到<add key='" + key + "'>的配置节");
return;
}
addKey.Attributes["value"].InnerText = strValue;
domWebConfig.Save(filepath);
MessageBox.Show("数据库连接配置成功","信息提示");
txtConnectionString.Text = connstr;
txtConnectionString.Enabled = false;
}
catch
{
// MessageBox.Show("在"+Environment.CurrentDirectory + "目录下找不到web.config配置文件","信息提示");
txtConnectionString.Enabled = true;
txtConnectionString.Clear();
txtConnectionString.ForeColor = Color.Red;
txtConnectionString.Text ="在" + Application.StartupPath + "目录下找不到App.config配置文件";
return;
} } private void buttonConn_Click(object sender, EventArgs e)
{
DataConnectionDialog dialog = new DataConnectionDialog();
//添加数据源列表,可以向窗口中添加自己程序所需要的数据源类型
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.DataSources.Add(DataSource.OdbcDataSource); dialog.SelectedDataSource = DataSource.SqlDataSource;
dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
//只能够通过DataConnectionDialog类的静态方法Show出对话框
//不同使用dialog.Show()或dialog.ShowDialog()来呈现对话框
if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK)
{
connstr = dialog.ConnectionString;
connstr = "Provider=SQLOLEDB.1;" + connstr;
Modify("ConnStr",connstr);
}
}
}
}