Display Key and Values of AppSettings To DataGridView From External Webconfig Using Vb.Net




Private Sub btnOpenFile_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenFile.Click
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then

            Dim appSetDT As DataTable
            appSetDT = New DataTable("AppSettings")

            Dim KeyNameCol As DataColumn = New DataColumn("KeyNameCol")
            KeyNameCol.DataType = System.Type.GetType("System.String")
            Dim KeyValueCol As DataColumn = New DataColumn("KeyValueCol")
            KeyValueCol.DataType = System.Type.GetType("System.String")
            appSetDT.Columns.Add(KeyNameCol)
            appSetDT.Columns.Add(KeyValueCol)

            Dim FileName As String = OpenFileDialog1.FileName.ToString()
            Dim map As New ExeConfigurationFileMap()
            map.ExeConfigFilename = FileName
            Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None)

            Dim appSettings = config.AppSettings
   
            Dim keyCount As Integer = appSettings.Settings.Count
            Dim keyName As String
            Dim keyValue As String

            'App Settings
            If appSettings.Settings.Count = 0 Then
                MsgBox("AppSettings is empty.", MsgBoxStyle.Information)
            Else
                For Each key In appSettings.Settings.AllKeys
                    keyName = key
                    keyValue = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).AppSettings.Settings(keyName).Value.ToString()        

                    Dim appRw As DataRow
                    appRw = appSetDT.NewRow()
                    appRw.Item("KeyNameCol") = keyName
                    appRw.Item("KeyValueCol") = keyValue
                    appSetDT.Rows.Add(appRw)
                Next

            End If
            'Load Data Table to Data Grid View
            dgvAppSettings.DataSource = appSetDT
            dgvAppSettings.Columns(0).Width = (dgvAppSettings.Width - 25) / 2
            dgvAppSettings.Columns(1).Width = (dgvAppSettings.Width - 25) / 2
            dgvAppSettings.Columns(0).HeaderText = "Key"
            dgvAppSettings.Columns(1).HeaderText = "Value"

     End If
    End Sub

No comments:

Post a Comment