Results 1 to 3 of 3

Thread: Storing an reading a collection of App Settings...

  1. #1

    Thread Starter
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616

    Storing an reading a collection of App Settings...

    OK - I have an App.config file and am able to read single items in name/value pairs thus:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
          <!-- Monitor level...1=Max, 2=Min, 3=None-->
          <add key="MonitorJobEventInformationLevel" value="1"/>
          <!-- Monitor Printer Change Events? 0=No, -1 = Yes -->
          <add key="MonitorPrinterChangeEvent" value="-1"/>
          <!-- Monitor Job Added Events? 0=No, -1 = Yes -->
          <add key="MonitorJobAddedEvent" value="-1"/>
          <!-- Monitor Job Deleted Events? 0=No, -1 = Yes -->
          <add key="MonitorDeletedEvent" value="-1"/>
          <!-- Monitor Job Set Events? 0=No, -1 = Yes -->
          <add key="MonitorJobSetEvent" value="-1"/>
          <!-- Monitor Job Written Events? 0=No, -1 = Yes -->
          <add key="MonitorJobWrittenEvent" value="-1"/>
       </appSettings>
    </configuration>
    which is accessed by:
    VB Code:
    1. Dim foo As Integer = System.Configuration.AppSettings.GetSetting("MonitorJobEventInformationLevel")

    But I also have a collection (of printer names, for example) and I want to store and access this in the config file. How can this be done?

    Currently I have a workaround which is:
    Code:
          <add key="MonitoredPrintersCount" value="2"/>
          <add key="MonitoredPrinter1" value="HP LaserJet 5L"/>
          <add key="MonitoredPrinter2" value="Microsoft Office Document Image Writer"/>
    but I would prefer a more XML approach e.g.:
    Code:
          <monitoredprinters>
             <add DeviceName="HP LaserJet 5L"/>
             <add DeviceName="Microsoft Office Document Image Writer"/>
          </monitoredprinters>
    Thanks in advance,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  2. #2

  3. #3

    Thread Starter
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Thus if the config file is like:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <configSections>
          <!-- Extra custom configuration sections -->
          <section name="monitoredPrinters"            type="PrinterMonitorService.CustomConfigurationSectionReaders.MonitoredPrintersSectionReader, PrinterMonitorService" />         
       </configSections>
           
        <monitoredPrinters>
             <!-- DeviceName = The unique name by which the printer is known
               -->
             <monitoredPrinter DeviceName="HP LaserJet 5L"/>
             <monitoredPrinter DeviceName="Microsoft Office Document Image Writer"/>
        </monitoredPrinters>
          
    </configuration>
    Then the class to read it could look like:-
    VB Code:
    1. Public NotInheritable Class MonitoredPrintersSectionReader
    2.         Implements IConfigurationSectionHandler
    3.  
    4. #Region "Private constants"
    5.         Private Const SECTION_NAME As String = "monitoredPrinters"
    6.         Private Const ATTRIBUTE_NAME As String = "monitoredPrinter"
    7. #End Region
    8.  
    9.         Shared Sub New()
    10.             ConfigurationSettings.GetConfig(SECTION_NAME)
    11.         End Sub
    12.  
    13.         Public Shared Function GetMonitoredPrintersCollection() As Collection
    14.             Return CType(System.Configuration.ConfigurationSettings.GetConfig(SECTION_NAME), Collection)
    15.         End Function
    16.  
    17. #Region "IConfigurationSectionHandler interface"
    18.         Public Function Create( _
    19.                  ByVal parent As Object, _
    20.                  ByVal configContext As Object, _
    21.                  ByVal section As XmlNode _
    22.                  ) As Object Implements IConfigurationSectionHandler.Create
    23.  
    24.  
    25.             Dim retVal As New Collection()
    26.  
    27.             If section.HasChildNodes Then
    28.                 Dim childNode As XmlNode
    29.                 For Each childNode In section.ChildNodes
    30.                     If childNode.Name = ATTRIBUTE_NAME Then
    31.                         retVal.Add(childNode.Attributes.ItemOf("DeviceName").Value)
    32.                     End If
    33.                 Next
    34.             End If
    35.  
    36.             Return retVal
    37.         End Function
    38. #End Region
    39.  
    40.         Private Sub New()
    41.             '\\ make this class non creatable
    42.         End Sub
    43.     End Class

    Hope this is useful,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width