views:

4992

answers:

3

I have a custom application with a simple app.config specifying SQL Server name and Database, I want to prompt the user on application install for application configuration items and then update the app.config file.

I admit I'm totally new to setup projects and am looking for some guidance. Thank You Mark Koops

+2  A: 

check this out - Installer with a custom action for changing settings

Gulzar
+1  A: 

App.Config CAN be changed...however it exists in a location akin to HKEY___LOCAL_MACHINE i.e. the average user has read-only access.

So you will need to change it as an administrator - best time would be during installation, where you're (supposed to be) installing with enhanced permissions.

So create an Installer class, use a Custom Action in the setup project to pass in the user's choices (e.g. "/svr=[SERVER] /db=[DB] /uilevel=[UILEVEL]") and, in the AfterInstall event, change the App.Config file using something like:

Public Shared Property AppConfigSetting(ByVal SettingName As String) As Object
    Get
        Return My.Settings.PropertyValues(SettingName)
    End Get
    Set(ByVal value As Object)
        Dim AppConfigFilename As String = String.Concat(System.Reflection.Assembly.GetExecutingAssembly.Location, ".config")

        If (My.Computer.FileSystem.FileExists(AppConfigFilename)) Then
            Dim AppSettingXPath As String = String.Concat("/configuration/applicationSettings/", My.Application.Info.AssemblyName, ".My.MySettings/setting[@name='", SettingName, "']/value")

            Dim AppConfigXML As New System.Xml.XmlDataDocument
            With AppConfigXML
                .Load(AppConfigFilename)

                Dim DataNode As System.Xml.XmlNode = .SelectSingleNode(AppSettingXPath)

                If (DataNode Is Nothing) Then
                    Throw New Xml.XmlException(String.Format("Application setting not found ({0})!", AppSettingXPath))

                Else
                    DataNode.InnerText = value.ToString
                End If

                .Save(AppConfigFilename)
            End With

        Else
            Throw New IO.FileNotFoundException("App.Config file not found!", AppConfigFilename)
        End If

    End Set
End Property
AndrewD
+1  A: 

I had problems with the code Gulzar linked to on a 64 bit machine. I found the link below to be a simple solution to getting values from the config ui into the app.config.

http://raquila.com/software/configure-app-config-application-settings-during-msi-install/

MarkGr