我在运行时对APP.CONFIG感到生气 [英] I am getting mad with APP.CONFIG in run-time
问题描述
大家好,
主要平台是以Framework 2.0和VB .NET为主要语言.
Primary platform is Framework 2.0 and VB .NET as main language.
当我从VS IDE中执行EXE时,我修改了"GIT.exe.config",文件
何时 我使用VS IDE执行程序. 我修改了"GIT.vshost.exe.config"文件
When I execute my program using VS IDE I modify my "GIT.vshost.exe.config" file
我对此很生气.我只是只有这个功能,以改变我的连接字符串分配下
I'm getting mad with that. I just have only this function in order to alter my connectionstring allocated under
" appSettings "密钥:
"appSettings" key:
我应该编写哪种代码才能每次修改正确的文件?
How kind of code should I write in order to modify the correct file every time??
Public Function Cambiar_base_datos(ByVal basedatos As String, ByVal donde As String) As Boolean
Dim KeyName As String
Dim KeyValue As String = "Server='OFI99999\SQLEXPRESS';Initial Catalog=Talleres;User ID=usrTaller99;Password=9999;current language = spanish"
Dim KeyValue2 As String
Dim XmlDoc As New XmlDocument()
KeyName = donde
KeyValue2 = KeyValue.Replace("Initial Catalog=Talleres", "Initial Catalog=" & basedatos)
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
For Each xElement As XmlElement In XmlDoc.DocumentElement
If xElement.Name = "appSettings" Then
Try
For Each xNode As XmlNode In xElement.ChildNodes
If xNode.Attributes(0).Value = KeyName Then
xNode.Attributes(1).Value = KeyValue2
End If
Next
Catch ex As Exception
End Try
End If
Next
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
End Function
恩里克
推荐答案
您不应在运行时真正修改app.config.也就是说,上述代码有什么问题?在Visual Studio中执行代码时,*.vshost.exe会运行,因此调试器可以更快地启动.所以如果你从 在Visual Studio中,您的程序将修改* .vshost.exe.config,如果在Visual Studio外部运行,您的程序将在修改* .exe.config.
You shouldn't really modify the app.config at runtime. That said, what's the problem with the above code? When you execute your code from within Visual Studio the *.vshost.exe is run so the debugger can launch faster. So if you run from Visual Studio, your program will modify *.vshost.exe.config and if you run outside of Visual Studio your program will modify *.exe.config.
这篇关于我在运行时对APP.CONFIG感到生气的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!