none
I am getting mad with APP.CONFIG in run-time RRS feed

  • Question

  • Hi all of you,

    Primary platform is Framework 2.0 and VB .NET as main language.

    When I execute my EXE out of VS IDE I modify my "GIT.exe.config" file

    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" 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
    


    Enric


    Monday, July 16, 2012 3:25 PM

All replies

  • 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.

    Monday, July 16, 2012 3:57 PM
  • Just a question, why do you need change the app.config in runtime? for change the ConnectionString?
    Monday, July 16, 2012 4:29 PM
  • Not an answer to your question, however appSettings is the .NET 1.x way of storing configuration info.  You might have better luck using the classes added to ConfigurationManager namespace.  For one thing, custom configurations have a convenient "Save" method.


    This signature unintentionally left blank.

    Monday, July 16, 2012 7:04 PM
  • You can turn off Visual Studio hosting in your project settings. I usually do this.

    Also, you are better off serializing properties to a separate XML files if you change values at run time.

    The "user" section is the only thing you can change in the config file file and a "save" does not save these values to the config file, it saves them to some user app-section file. So these settings are only good for user's custom settings and not much else because you can't copy the app to another computer and keep these properties.


    Dan Randolph - My Code Samples List

    • Proposed as answer by Dan Randolph Thursday, September 27, 2012 10:43 PM
    Monday, July 16, 2012 10:13 PM
  • Hi, just for changing the Sql Server database. I get the "Initial Catalog=xxx"

    it is just for a specific tests for our customers.

    Thanks again,


    Enric

    Tuesday, July 17, 2012 6:52 AM
  • Hi, just for changing the Sql Server database. I get the "Initial Catalog=xxx"

    it is just for a specific tests for our customers.

    Thanks again,


    Enric

    Hi Enric,

    Welcome to the MSDN Forum.

    What is your meaning of this post? Is your issue gone away?

    As the members' suggestion, we don't change the config file at runtime for such purpose.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 17, 2012 10:45 AM
    Moderator