2005的app.config读取有个问题.我用string ss = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];在一个项目里虽然提示ConfigurationSettings过时,但是可以读出值; 另一个项目里就提示ConfigurationSettings过时,但就是读不出值;郁闷死.NND
这是我以前是1.1写的,vb的,你看看.其它的不作说明了 其中,configuration/appSettings/add 是从web.config的根元素开始的逐个节点查找 bind_controls() 这部分是取值 button1_click是赋值Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then bind_controls() End If End Sub Sub bind_controls() Dim aKey As String For Each aKey In ConfigurationSettings.AppSettings.Keys Select Case aKey Case "mail_subject" mail_subject.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings.Item(aKey)) Case "send_mail_user" send_mail_user.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings.Item(aKey)) Case "send_mail_pass" send_mail_pass.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings.Item(aKey)) Case "send_mail_smtp" send_mail_smtp.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings(aKey)) End Select Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Page.IsValid Then Dim doc As XmlDocument = New XmlDocument doc.Load(Server.MapPath("../" & "Web.config")) Dim root As XmlElement = doc.DocumentElement Dim n As XmlNode n = root.SelectSingleNode("/configuration/appSettings/add[@key='mail_subject']") n.Attributes("value").Value = Server.HtmlEncode(mail_subject.Text.Trim) n = root.SelectSingleNode("/configuration/appSettings/add[@key='send_mail_user']") n.Attributes("value").Value = Server.HtmlEncode(send_mail_user.Text.Trim) n = root.SelectSingleNode("/configuration/appSettings/add[@key='send_mail_pass']") n.Attributes("value").Value = Server.HtmlEncode(send_mail_pass.Text.Trim) n = root.SelectSingleNode("/configuration/appSettings/add[@key='send_mail_smtp']") n.Attributes("value").Value = Server.HtmlEncode(send_mail_smtp.Text.Trim) Try doc.Save(Server.MapPath("../" & "Web.config")) Label1.Text = "配置邮件成功" Catch ex As Exception Page.RegisterStartupScript("save_err", "<script language=""javascript"">alert('" & ex.Message.ToString.Replace("'", "") & "')</script>") Label1.Text = "配置邮件出现错误" End Try End If End Sub
1.1里面没看到什么TableAdapter。
2.0就不熟,但1.1里面的web.config更改了就会重启应用程序啊。
另一个项目里就提示ConfigurationSettings过时,但就是读不出值;郁闷死.NND
其中,configuration/appSettings/add 是从web.config的根元素开始的逐个节点查找
bind_controls() 这部分是取值
button1_click是赋值Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then
bind_controls()
End If End Sub Sub bind_controls() Dim aKey As String For Each aKey In ConfigurationSettings.AppSettings.Keys Select Case aKey
Case "mail_subject"
mail_subject.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings.Item(aKey))
Case "send_mail_user"
send_mail_user.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings.Item(aKey))
Case "send_mail_pass"
send_mail_pass.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings.Item(aKey))
Case "send_mail_smtp"
send_mail_smtp.Text = Server.HtmlDecode(System.Configuration.ConfigurationSettings.AppSettings(aKey))
End Select
Next
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Page.IsValid Then Dim doc As XmlDocument = New XmlDocument
doc.Load(Server.MapPath("../" & "Web.config"))
Dim root As XmlElement = doc.DocumentElement Dim n As XmlNode
n = root.SelectSingleNode("/configuration/appSettings/add[@key='mail_subject']")
n.Attributes("value").Value = Server.HtmlEncode(mail_subject.Text.Trim) n = root.SelectSingleNode("/configuration/appSettings/add[@key='send_mail_user']")
n.Attributes("value").Value = Server.HtmlEncode(send_mail_user.Text.Trim) n = root.SelectSingleNode("/configuration/appSettings/add[@key='send_mail_pass']")
n.Attributes("value").Value = Server.HtmlEncode(send_mail_pass.Text.Trim) n = root.SelectSingleNode("/configuration/appSettings/add[@key='send_mail_smtp']")
n.Attributes("value").Value = Server.HtmlEncode(send_mail_smtp.Text.Trim) Try
doc.Save(Server.MapPath("../" & "Web.config"))
Label1.Text = "配置邮件成功"
Catch ex As Exception
Page.RegisterStartupScript("save_err", "<script language=""javascript"">alert('" & ex.Message.ToString.Replace("'", "") & "')</script>")
Label1.Text = "配置邮件出现错误"
End Try End If End Sub
但是,这个value 因为是全局(application)的Settings,所以,不能用程序修改,只能在设计是通过DLL或EXE工程的
Properties下的Settings.Settings
来设定,
楼主指的是在vs 2005中开发应用程序出现问题,我一开始也碰到这个问题,一直没有想到很好的解决方法(估计是有),下面是我那时开发项目的情况,
====================
由于数据库访问代码是一个独立的组件项目,而且我通过数据源工具窗口生成一个强类型的DataSet,
结果会在项目Properties节点下生成一个Settings.settings文件,保存了数据库连接字符串。
通过强类型DataSet为里面的数据表添加了TableAdapter,选中DataTable对应的TableAdapter,
在属性窗口中有个ConnectionModifier属性,用来设置连接的可见性级别,默认是Internal,
因此在逻辑层引用该数据层时,想要修改该数据链接字符窜代码却无法访问。
比如:
TableAdapter1.Connection.ConnectionString = connString;
,而在开发阶段生成的数据库连接字符串一般都是测试系统的。要修改一种实现方式是通过修改
ConnectionModifier属性设置为Public。这样就在其它项目中直接修改。
另一种方式是:
在数据层项目中增加一个辅助类,类似如下代码,
public class SqlHeper
{
//..........
public string SetConnectionString(string connString)
{
TableAdapter1.Connection.ConnectionString = connString;
}
}
在其他项目中通过访问该辅助类来修改TableAdapter中的数据连接串。
如下配置文件,记录了主窗体起始位置,
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<applicationSettings>
<NbEportEms.ERP.Properties.Settings>
<setting name="退出" serializeAs="String">
<value>XToolStripMenuItem</value>
</setting>
<setting name="ApplicationName" serializeAs="String">
<value>电子帐册ERP数据提取服务</value>
</setting>
</NbEportEms.ERP.Properties.Settings>
</applicationSettings>
<userSettings>
<NbEportEms.ERP.Properties.Settings>
<setting name="MainForm_Location" serializeAs="String">
<value>0, 0</value>
</setting>
</NbEportEms.ERP.Properties.Settings>
</userSettings>
</configuration>
在应用程序退出后,虽然该窗体位置已经变动,但是不会自动记录到配置文件。