In Windows applications written before the advent of .NET, developers used various methods for providing configuration information to applications, such as .INI files, System Registry, etc. All .NET applications are designed to read configuration from either the app.config (Winform or console apps) or web.config (web apps) file. This normally means the developer adds configuration data to the appSettings section of the app.config file, as shown below.
| <appSettings> <add key="myKey" value="myValue" /> </appsettings> |
| Dim value As String = ConfigurationSettings.AppSettings("myKey") |
| <configuration> <appSettings> <!-- Add Customer keys with Name|Account|PW --> <add key = "NUMBERCUSTOMERS" value = "2" /> <add key = "CUSTOMER001" value="Jones Co|JONESUser|!23*(Abc1" /> <add key = "CUSTOMER002" value= "NYTA|NYTAUserid|y*hg01K" /> </appSettings> </configuration> |
| ''' <summary> ''' The App Config file will have multiple CUSTNBRnnn keys in appsettings. ''' It will also have a NumberCustomers key with value = nbr of CustNbr occurrences. ''' Retrieve the number and use it to compute the value of each CustNbrnnn key value. ''' This must be done because the ConfigurationSettings.AppSettings method only picks up ''' the last value of a duplicate key name (bummer). This is because the code that builds ''' the NamedValueCollection stores into the collection, if a value exists, instead of ''' adding to the collection. It's built like an old VB Collection. I.,e., you can't have ''' but one occurrence of the same key name. But we wlll not be daunted... ''' </summary> ''' <remarks></remarks> Private Sub FillCustomerCollection() Dim nbrCusts As Integer = _ CType(ConfigurationSettings.AppSettings("NUMBERCUSTOMERS"), Integer) colCustomers = New Collection For i As Integer = 1 To nbrCusts Dim custKey As String = "CUSTOMER" & Format(i, "000") Dim custValue As String = ConfigurationSettings.AppSettings(custKey) Dim cust As New CustAcctPW Dim m As Match = Regex.Match(custValue, _ "(?<cust>\w+)\|(?<acct>\w+)\|(?<pw>\w+)") If m.Success Then cust.Customer = m.Groups("cust").Value cust.Account = m.Groups("acct").Value cust.PW = m.Groups("pw").Value colCustomers.Add(cust) End If Next End Sub Private colCustomers As Collection Public Class CustAcctPW Public Customer As String Public Account As String Public PW As String End Class |
| ' Fill the customer combo box with customer numbers from the ' colCustomers collection. Private Sub FillCustomerCombo(ByVal colCustomers As Collection) Me.cboSelectCustomer.Items.Clear() For i As Integer = 1 To colCustomers.Count Dim cust As CustAcctPW = CType(colCustomers(i), CustAcctPW) Me.cboSelectCustomer.Items.Add(cust.Customer) Next End Sub |
| Private Sub cboSelectCustomer_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cboSelectCustomer.SelectedIndexChanged If formLoading Then Exit Sub For i As Integer = 1 To colCustomers.Count Dim cust As CustAcctPW = CType(colCustomers(i), CustAcctPW) If cust.Customer = cboSelectCustomer.Text Then Me.txtAccount.Text = cust.Account Me.txtPassword.Text = cust.PW End If Next End Sub |