Advertisement
PiToLoKo

resx manager

Mar 18th, 2015
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 33.22 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <SnippetTypes>
  6.         <SnippetType>Expansion</SnippetType>
  7.       </SnippetTypes>
  8.       <Title>
  9.          ResX Manager
  10.       </Title>
  11.       <Author>Elektro H@cker</Author>
  12.       <Description>
  13.          Manages a .Net managed resource file (ResX).
  14.       </Description>
  15.       <HelpUrl>
  16.       </HelpUrl>
  17.       <Shortcut>
  18.       </Shortcut>
  19.     </Header>
  20.     <Snippet>
  21.       <Declarations>
  22.         <Literal Editable="true">
  23.           <ID>aaaaaaaaa</ID>
  24.           <ToolTip>sfsdf</ToolTip>
  25.           <Default>
  26.           </Default>
  27.           <Function>sdfsdf</Function>
  28.         </Literal>
  29.       </Declarations>
  30.       <Code Language="vb"><![CDATA[
  31.      
  32. ' ***********************************************************************
  33. ' Author   : Elektro
  34. ' Modified : 16-March-2015
  35. ' ***********************************************************************
  36. ' <copyright file="ResXManager.vb" company="Elektro Studios">
  37. '     Copyright (c) Elektro Studios. All rights reserved.
  38. ' </copyright>
  39. ' ***********************************************************************
  40.  
  41. #Region " Option Statements "
  42.  
  43. Option Strict On
  44. Option Explicit On
  45. Option Infer Off
  46.  
  47. #End Region
  48.  
  49. #Region " Usage Examples "
  50.  
  51. 'Imports System.IO
  52. 'Imports System.Text
  53.  
  54. 'Public Class Form1
  55.  
  56. '    Private Sub Test() Handles MyBase.Shown
  57.  
  58. '        Dim resX As New ResXManager(Path.Combine(Application.StartupPath, "MyResources.resx"))
  59. '        With resX
  60.  
  61. '            ' Create or replace the ResX file.
  62. '            .Create(replace:=True)
  63.  
  64. '            ' Add a string resource.
  65. '            .AddResource(Of String)("String Resource", "Hello World!", "String Comment")
  66. '            ' Add a bitmap resource.
  67. '            .AddResource(Of Bitmap)("Bitmap Resource", SystemIcons.Information.ToBitmap, "Bitmap Comment")
  68. '            ' Add a binary resource.
  69. '            .AddResource(Of Byte())("Binary Resource", File.ReadAllBytes("C:\file.mp3"), "Binary Comment")
  70.  
  71. '        End With
  72.  
  73. '        ' *******************************************************************************************************
  74.  
  75. '        ' Get the string resource.
  76. '        Dim stringResource As ResXManager.Resource(Of String) =
  77. '            resX.FindResource(Of String)("String Resource", StringComparison.OrdinalIgnoreCase)
  78.  
  79. '        ' Get the bitmap resource.
  80. '        Dim bitmapResource As ResXManager.Resource(Of Bitmap) =
  81. '            resX.FindResource(Of Bitmap)("Bitmap Resource", StringComparison.OrdinalIgnoreCase)
  82.  
  83. '        ' Get the binary resource.
  84. '        Dim binaryResource As ResXManager.Resource(Of Byte()) =
  85. '            resX.FindResource(Of Byte())("Binary Resource", StringComparison.OrdinalIgnoreCase)
  86.  
  87. '        ' *******************************************************************************************************
  88.  
  89. '        ' Get the string data.
  90. '        Dim stringData As String = stringResource.Data
  91.  
  92. '        ' Get the bitmap data.
  93. '        Dim bitmapData As Bitmap = bitmapResource.Data
  94.  
  95. '        ' Get the binary data.
  96. '        Dim binaryData As Byte() = binaryResource.Data
  97.  
  98. '        ' *******************************************************************************************************
  99.  
  100. '        ' Get all the resources at once.
  101. '        Dim resources As IEnumerable(Of ResXManager.Resource) = resX.Resources
  102.  
  103. '        ' Get all the resources of specific Type at once.
  104. '        Dim stringResources As IEnumerable(Of ResXManager.Resource(Of String)) = resX.FindResources(Of String)()
  105.  
  106. '        ' *******************************************************************************************************
  107.  
  108. '        ' Get all the resource datas at once from Resource collection.
  109. '        Dim resourceDatas As IEnumerable(Of Object) =
  110. '            From res As ResXManager.Resource In resX.Resources
  111. '            Select res.Data
  112.  
  113. '        ' Get all the resource datas of specific Type at once from Resource collection.
  114. '        Dim stringResourceDatas As IEnumerable(Of String) =
  115. '            From res As ResXManager.Resource In resX.Resources
  116. '            Where res.Type Is GetType(String)
  117. '            Select DirectCast(res.Data, String)
  118.  
  119. '        ' *******************************************************************************************************
  120.  
  121. '        ' Treat the string data as you like.
  122. '        MessageBox.Show(stringData, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
  123.  
  124. '        ' Treat the bitmap data as you like.
  125. '        Me.Icon = Icon.FromHandle(bitmapData.GetHicon)
  126.  
  127. '        ' Treat the binary data as you like.
  128. '        File.WriteAllBytes("C:\new file.mp3", binaryData)
  129.  
  130. '        ' *******************************************************************************************************
  131.  
  132. '        ' Iterate all the resources.
  133. '        For Each res As ResXManager.Resource In resX.Resources
  134.  
  135. '            Dim sb As New StringBuilder
  136.  
  137. '            sb.AppendLine(String.Format("Name...: {0}", res.Name))
  138. '            sb.AppendLine(String.Format("Comment: {0}", res.Comment))
  139. '            sb.AppendLine(String.Format("Type...: {0}", res.Type.ToString))
  140. '            sb.AppendLine(String.Format("Data...: {0}", res.Data.ToString))
  141.  
  142. '            MsgBox(sb.ToString)
  143. '        Next
  144.  
  145. '        ' Iterate all the resources of specific Type.
  146. '        For Each res As ResXManager.Resource(Of String) In resX.FindResources(Of String)()
  147.  
  148. '            Dim sb As New StringBuilder
  149.  
  150. '            sb.AppendLine(String.Format("Name...: {0}", res.Name))
  151. '            sb.AppendLine(String.Format("Comment: {0}", res.Comment))
  152. '            sb.AppendLine(String.Format("Type...: {0}", res.Type.ToString))
  153. '            sb.AppendLine(String.Format("Data...: {0}", res.Data.ToString))
  154.  
  155. '            MsgBox(sb.ToString)
  156. '        Next
  157.  
  158. '        ' *******************************************************************************************************
  159.  
  160. '        ' Remove a resource.
  161. '        resX.RemoveResource("Binary Resource")
  162.  
  163. '        '  GC.Collect()
  164.  
  165. '    End Sub
  166.  
  167. 'End Class
  168.  
  169. #End Region
  170.  
  171. #Region " Imports "
  172.  
  173. Imports System.ComponentModel
  174. Imports System.ComponentModel.Design
  175. Imports System.IO
  176. Imports System.Resources
  177.  
  178. #End Region
  179.  
  180. ''' <summary>
  181. ''' Manages a .Net managed resource file.
  182. ''' </summary>
  183. Public NotInheritable Class ResXManager
  184.  
  185. #Region " Properties "
  186.  
  187.     ''' <summary>
  188.     ''' Gets the .Net managed resource file path.
  189.     ''' </summary>
  190.     ''' <value>The .Net managed resource filepath.</value>
  191.     Public ReadOnly Property FilePath As String
  192.         Get
  193.             Return Me.filePath1
  194.         End Get
  195.     End Property
  196.     ''' <summary>
  197.     ''' The .Net managed resource file path.
  198.     ''' </summary>
  199.     Private ReadOnly filePath1 As String
  200.  
  201.     ''' <summary>
  202.     ''' Gets the resources contained in the .Net managed resource file.
  203.     ''' </summary>
  204.     ''' <value>The resources.</value>
  205.     Public ReadOnly Property Resources As IEnumerable(Of Resource)
  206.         Get
  207.             Return GetResources()
  208.         End Get
  209.     End Property
  210.  
  211. #End Region
  212.  
  213. #Region " Types "
  214.  
  215. #Region " Resource "
  216.  
  217.     ''' <summary>
  218.     ''' Defines a resource of a .Net managed resource file.
  219.     ''' </summary>
  220.     <Serializable>
  221.     Public NotInheritable Class Resource
  222.  
  223. #Region " Properties "
  224.  
  225.         ''' <summary>
  226.         ''' Gets the resource name.
  227.         ''' </summary>
  228.         ''' <value>The resource name.</value>
  229.         Public ReadOnly Property Name As String
  230.             Get
  231.                 Return Me.name1
  232.             End Get
  233.         End Property
  234.         Private ReadOnly name1 As String
  235.  
  236.         ''' <summary>
  237.         ''' Gets the resource data.
  238.         ''' </summary>
  239.         ''' <value>The resource data.</value>
  240.         Public ReadOnly Property Data As Object
  241.             Get
  242.                 Return Me.data1
  243.             End Get
  244.         End Property
  245.         Private ReadOnly data1 As Object
  246.  
  247.         ''' <summary>
  248.         ''' Gets the resource type.
  249.         ''' </summary>
  250.         ''' <value>The resource type.</value>
  251.         Public ReadOnly Property Type As Type
  252.             Get
  253.                 Return Data.GetType
  254.             End Get
  255.         End Property
  256.  
  257.         ''' <summary>
  258.         ''' Gets the resource comment.
  259.         ''' </summary>
  260.         ''' <value>The resource comment.</value>
  261.         Public ReadOnly Property Comment As String
  262.             Get
  263.                 Return comment1
  264.             End Get
  265.         End Property
  266.         Private ReadOnly comment1 As String
  267.  
  268.         ''' <summary>
  269.         ''' Represents a <see cref="Resource"/> instance that is <c>Nothing</c>.
  270.         ''' </summary>
  271.         ''' <value><c>Nothing</c></value>
  272.         <EditorBrowsable(EditorBrowsableState.Advanced)>
  273.         Public Shared ReadOnly Property Empty As Resource
  274.             Get
  275.                 Return Nothing
  276.             End Get
  277.         End Property
  278.  
  279. #End Region
  280.  
  281. #Region " Constructors "
  282.  
  283.         ''' <summary>
  284.         ''' Initializes a new instance of the <see cref="Resource"/> class.
  285.         ''' </summary>
  286.         ''' <param name="name">The resource name.</param>
  287.         ''' <param name="data">The resource data.</param>
  288.         ''' <param name="comment">The resource comment.</param>
  289.         Public Sub New(ByVal name As String,
  290.                        ByVal data As Object,
  291.                        ByVal comment As String)
  292.  
  293.             Me.name1 = name
  294.             Me.data1 = data
  295.             Me.comment1 = comment
  296.  
  297.         End Sub
  298.  
  299.         ''' <summary>
  300.         ''' Prevents a default instance of the <see cref="Resource"/> class from being created.
  301.         ''' </summary>
  302.         Private Sub New()
  303.         End Sub
  304.  
  305. #End Region
  306.  
  307. #Region " Hidden Methods "
  308.  
  309.         ''' <summary>
  310.         ''' Determines whether the specified System.Object instances are considered equal.
  311.         ''' </summary>
  312.         <EditorBrowsable(EditorBrowsableState.Never)>
  313.         Public Shadows Function Equals(ByVal obj As Object) As Boolean
  314.             Return MyBase.Equals(obj)
  315.         End Function
  316.  
  317.         ''' <summary>
  318.         ''' Serves as a hash function for a particular type.
  319.         ''' </summary>
  320.         <EditorBrowsable(EditorBrowsableState.Never)>
  321.         Public Shadows Function GetHashCode() As Integer
  322.             Return MyBase.GetHashCode
  323.         End Function
  324.  
  325.         ''' <summary>
  326.         ''' Gets the System.Type of the current instance.
  327.         ''' </summary>
  328.         ''' <returns>The exact runtime type of the current instance.</returns>
  329.         <EditorBrowsable(EditorBrowsableState.Never)>
  330.         Public Shadows Function [GetType]() As Type
  331.             Return MyBase.GetType
  332.         End Function
  333.  
  334.         ''' <summary>
  335.         ''' Returns a String that represents the current object.
  336.         ''' </summary>
  337.         <EditorBrowsable(EditorBrowsableState.Never)>
  338.         Public Shadows Function ToString() As String
  339.             Return MyBase.ToString
  340.         End Function
  341.  
  342. #End Region
  343.  
  344.     End Class
  345.  
  346. #End Region
  347.  
  348. #Region " Resource(Of T) "
  349.  
  350.     ''' <summary>
  351.     ''' Defines a resource of a .Net managed resource file.
  352.     ''' </summary>
  353.     <Serializable>
  354.     Public NotInheritable Class Resource(Of T)
  355.  
  356. #Region " Properties "
  357.  
  358.         ''' <summary>
  359.         ''' Gets the resource name.
  360.         ''' </summary>
  361.         ''' <value>The resource name.</value>
  362.         Public ReadOnly Property Name As String
  363.             Get
  364.                 Return Me.name1
  365.             End Get
  366.         End Property
  367.         Private ReadOnly name1 As String
  368.  
  369.         ''' <summary>
  370.         ''' Gets the resource data.
  371.         ''' </summary>
  372.         ''' <value>The resource data.</value>
  373.         Public ReadOnly Property Data As T
  374.             Get
  375.                 Return Me.data1
  376.             End Get
  377.         End Property
  378.         Private ReadOnly data1 As T
  379.  
  380.         ''' <summary>
  381.         ''' Gets the resource type.
  382.         ''' </summary>
  383.         ''' <value>The resource type.</value>
  384.         Public ReadOnly Property Type As Type
  385.             Get
  386.                 Return GetType(T)
  387.             End Get
  388.         End Property
  389.  
  390.         ''' <summary>
  391.         ''' Gets the resource comment.
  392.         ''' </summary>
  393.         ''' <value>The resource comment.</value>
  394.         Public ReadOnly Property Comment As String
  395.             Get
  396.                 Return comment1
  397.             End Get
  398.         End Property
  399.         Private ReadOnly comment1 As String
  400.  
  401. #End Region
  402.  
  403. #Region " Constructors "
  404.  
  405.         ''' <summary>
  406.         ''' Initializes a new instance of the <see cref="Resource(Of T)"/> class.
  407.         ''' </summary>
  408.         ''' <param name="name">The resource name.</param>
  409.         ''' <param name="data">The resource data.</param>
  410.         ''' <param name="comment">The resource comment.</param>
  411.         Public Sub New(ByVal name As String,
  412.                        ByVal data As T,
  413.                        ByVal comment As String)
  414.  
  415.             Me.name1 = name
  416.             Me.data1 = data
  417.             Me.comment1 = comment
  418.  
  419.         End Sub
  420.  
  421.         ''' <summary>
  422.         ''' Prevents a default instance of the <see cref="Resource(Of T)"/> class from being created.
  423.         ''' </summary>
  424.         Private Sub New()
  425.         End Sub
  426.  
  427. #End Region
  428.  
  429. #Region " Hidden Methods "
  430.  
  431.         ''' <summary>
  432.         ''' Determines whether the specified System.Object instances are considered equal.
  433.         ''' </summary>
  434.         <EditorBrowsable(EditorBrowsableState.Never)>
  435.         Public Shadows Function Equals(ByVal obj As Object) As Boolean
  436.             Return MyBase.Equals(obj)
  437.         End Function
  438.  
  439.         ''' <summary>
  440.         ''' Serves as a hash function for a particular type.
  441.         ''' </summary>
  442.         <EditorBrowsable(EditorBrowsableState.Never)>
  443.         Public Shadows Function GetHashCode() As Integer
  444.             Return MyBase.GetHashCode
  445.         End Function
  446.  
  447.         ''' <summary>
  448.         ''' Gets the System.Type of the current instance.
  449.         ''' </summary>
  450.         ''' <returns>The exact runtime type of the current instance.</returns>
  451.         <EditorBrowsable(EditorBrowsableState.Never)>
  452.         Public Shadows Function [GetType]() As Type
  453.             Return MyBase.GetType
  454.         End Function
  455.  
  456.         ''' <summary>
  457.         ''' Returns a String that represents the current object.
  458.         ''' </summary>
  459.         <EditorBrowsable(EditorBrowsableState.Never)>
  460.         Public Shadows Function ToString() As String
  461.             Return MyBase.ToString
  462.         End Function
  463.  
  464. #End Region
  465.  
  466.     End Class
  467.  
  468. #End Region
  469.  
  470. #End Region
  471.  
  472. #Region " Constructors "
  473.  
  474.     ''' <summary>
  475.     ''' Initializes a new instance of the <see cref="ResXManager"/> class.
  476.     ''' </summary>
  477.     ''' <param name="resxFilePath">The .Net managed resource filepath.</param>
  478.     Public Sub New(ByVal resxFilePath As String)
  479.         Me.filePath1 = resxFilePath
  480.     End Sub
  481.  
  482.     ''' <summary>
  483.     ''' Prevents a default instance of the <see cref="ResXManager"/> class from being created.
  484.     ''' </summary>
  485.     Private Sub New()
  486.     End Sub
  487.  
  488. #End Region
  489.  
  490. #Region " Public Methods "
  491.  
  492.     ''' <summary>
  493.     ''' Creates the .Net managed resource file.
  494.     ''' </summary>
  495.     ''' <param name="replace">if set to <c>true</c>, replaces any existent file.</param>
  496.     ''' <exception cref="System.Exception"></exception>
  497.     Public Sub Create(Optional ByVal replace As Boolean = False)
  498.  
  499.         If Not replace AndAlso File.Exists(Me.filePath1) Then
  500.             Throw New Exception(String.Format("Resource file already exists: {0}", Me.filePath1))
  501.             Exit Sub
  502.         End If
  503.  
  504.         Dim resXWritter As ResXResourceWriter = Nothing
  505.         Try
  506.             resXWritter = New ResXResourceWriter(Me.filePath1)
  507.             Using resXWritter
  508.                 resXWritter.Generate()
  509.             End Using
  510.  
  511.         Catch ex As Exception
  512.             Throw
  513.  
  514.         Finally
  515.             If resXWritter IsNot Nothing Then
  516.                 resXWritter.Close()
  517.             End If
  518.  
  519.         End Try
  520.  
  521.     End Sub
  522.  
  523.     ''' <summary>
  524.     ''' Adds a resource into the .Net managed resource file.
  525.     ''' </summary>
  526.     ''' <param name="name">The resource name.</param>
  527.     ''' <param name="data">The resource data.</param>
  528.     ''' <param name="comment">The resource comment.</param>
  529.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  530.     ''' <exception cref="System.ArgumentException">A resource with the same name already exists in the table.;name</exception>
  531.     Public Sub AddResource(ByVal name As String,
  532.                            ByVal data As Object,
  533.                            Optional ByVal comment As String = Nothing)
  534.  
  535.         Me.AddResource(replace:=False, name:=name, data:=data, comment:=comment)
  536.  
  537.     End Sub
  538.  
  539.     ''' <summary>
  540.     ''' Adds a specified resource of the specified type into the .Net managed resource file.
  541.     ''' </summary>
  542.     ''' <typeparam name="T"></typeparam>
  543.     ''' <param name="name">The resource name.</param>
  544.     ''' <param name="data">The resource data.</param>
  545.     ''' <param name="comment">The resource comment.</param>
  546.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  547.     ''' <exception cref="System.ArgumentException">A resource with the same name already exists in the table.;name</exception>
  548.     Public Sub AddResource(Of T)(ByVal name As String,
  549.                                  ByVal data As T,
  550.                                  Optional ByVal comment As String = Nothing)
  551.  
  552.         Me.AddResource(replace:=False, name:=name, data:=data, comment:=comment)
  553.  
  554.     End Sub
  555.  
  556.     ''' <summary>
  557.     ''' Replaces a resource by the specified name inside the .Net managed resource file.
  558.     ''' </summary>
  559.     ''' <param name="name">The resource name.</param>
  560.     ''' <param name="data">The resource data.</param>
  561.     ''' <param name="comment">The resource comment.</param>
  562.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  563.     ''' <exception cref="System.ArgumentException">A resource with the same name already exists in the table.;name</exception>
  564.     Public Sub ReplaceResource(ByVal name As String,
  565.                                ByVal data As Object,
  566.                                Optional ByVal comment As String = Nothing)
  567.  
  568.         Me.AddResource(replace:=True, name:=name, data:=data, comment:=comment)
  569.  
  570.     End Sub
  571.  
  572.     ''' <summary>
  573.     ''' Replaces a resource by the specified name of the specified type inside the .Net managed resource file.
  574.     ''' </summary>
  575.     ''' <typeparam name="T"></typeparam>
  576.     ''' <param name="name">The resource name.</param>
  577.     ''' <param name="data">The resource data.</param>
  578.     ''' <param name="comment">The resource comment.</param>
  579.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  580.     ''' <exception cref="System.ArgumentException">A resource with the same name already exists in the table.;name</exception>
  581.     Public Sub ReplaceResource(Of T)(ByVal name As String,
  582.                                      ByVal data As T,
  583.                                      Optional ByVal comment As String = Nothing)
  584.  
  585.         Me.AddResource(replace:=True, name:=name, data:=data, comment:=comment)
  586.  
  587.     End Sub
  588.  
  589.     ''' <summary>
  590.     ''' Finds a resource by the specified name of specified type inside the .Net managed resource file.
  591.     ''' </summary>
  592.     ''' <typeparam name="T"></typeparam>
  593.     ''' <param name="name">The resource name.</param>
  594.     ''' <param name="stringComparison">The <see cref="StringComparison"/> to compare the resource name.</param>
  595.     ''' <returns>The resource.</returns>
  596.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  597.     ''' <exception cref="System.ArgumentException">Resource with the specified name is not found.;name</exception>
  598.     ''' <exception cref="System.ArgumentException">The specified Type differs from the resource Type.;T</exception>
  599.     Public Function FindResource(Of T)(ByVal name As String,
  600.                                        Optional ByVal stringComparison As StringComparison =
  601.                                                       StringComparison.OrdinalIgnoreCase) As Resource(Of T)
  602.  
  603.         If Not File.Exists(Me.filePath1) Then
  604.             Throw New FileNotFoundException("Resource file not found.", Me.filePath1)
  605.             Exit Function
  606.         End If
  607.  
  608.         ' Read the ResX file.
  609.         Dim resX As ResXResourceReader = Nothing
  610.         Dim res As Resource(Of T) = Nothing
  611.         Try
  612.             resX = New ResXResourceReader(Me.filePath1) With {.UseResXDataNodes = True}
  613.             Using resX
  614.  
  615.                 For Each entry As DictionaryEntry In resX
  616.  
  617.                     If entry.Key.ToString.Equals(name, stringComparison) Then
  618.  
  619.                         Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
  620.  
  621.                         res = New Resource(Of T)(name:=node.Name,
  622.                                                  data:=DirectCast(node.GetValue(DirectCast(Nothing, ITypeResolutionService)), T),
  623.                                                  comment:=node.Comment)
  624.                         Exit For
  625.  
  626.                     End If
  627.  
  628.                 Next entry
  629.  
  630.             End Using ' resX
  631.  
  632.             Return res
  633.  
  634.         Catch ex As Exception
  635.             Throw
  636.  
  637.         Finally
  638.             If resX IsNot Nothing Then
  639.                 resX.Close()
  640.             End If
  641.  
  642.         End Try
  643.  
  644.     End Function
  645.  
  646.     ''' <summary>
  647.     ''' Finds a resource by the specified name inside the .Net managed resource file.
  648.     ''' </summary>
  649.     ''' <param name="name">The resource name.</param>
  650.     ''' <param name="stringComparison">The <see cref="StringComparison"/> to compare the resource name.</param>
  651.     ''' <returns>The resource.</returns>
  652.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  653.     ''' <exception cref="System.ArgumentException">Resource with the specified name is not found.;name</exception>
  654.     ''' <exception cref="System.ArgumentException">The specified Type differs from the resource Type.;T</exception>
  655.     Public Function FindResource(ByVal name As String,
  656.                                  Optional ByVal stringComparison As StringComparison =
  657.                                                 StringComparison.OrdinalIgnoreCase) As Resource
  658.  
  659.         If Not File.Exists(Me.filePath1) Then
  660.             Throw New FileNotFoundException("Resource file not found.", Me.filePath1)
  661.             Exit Function
  662.         End If
  663.  
  664.         ' Read the ResX file.
  665.         Dim resX As ResXResourceReader = Nothing
  666.         Dim res As Resource = Nothing
  667.         Try
  668.             resX = New ResXResourceReader(Me.filePath1) With {.UseResXDataNodes = True}
  669.             Using resX
  670.  
  671.                 For Each entry As DictionaryEntry In resX
  672.  
  673.                     If entry.Key.ToString.Equals(name, stringComparison) Then
  674.  
  675.                         Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
  676.  
  677.                         res = New Resource(name:=node.Name,
  678.                                            data:=node.GetValue(DirectCast(Nothing, ITypeResolutionService)),
  679.                                            comment:=node.Comment)
  680.                         Exit For
  681.  
  682.                     End If
  683.  
  684.                 Next entry
  685.  
  686.             End Using ' resX
  687.  
  688.             Return res
  689.  
  690.         Catch ex As Exception
  691.             Throw
  692.  
  693.         Finally
  694.             If resX IsNot Nothing Then
  695.                 resX.Close()
  696.             End If
  697.  
  698.         End Try
  699.  
  700.     End Function
  701.  
  702.     ''' <summary>
  703.     ''' Finds the resources of the specified type inside the .Net managed resource file.
  704.     ''' </summary>
  705.     ''' <typeparam name="T"></typeparam>
  706.     ''' <returns>The resource.</returns>
  707.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  708.     ''' <exception cref="System.ArgumentException">Resource with the specified name is not found.;name</exception>
  709.     ''' <exception cref="System.ArgumentException">The specified Type differs from the resource Type.;T</exception>
  710.     Public Iterator Function FindResources(Of T)() As IEnumerable(Of Resource(Of T))
  711.  
  712.         If Not File.Exists(Me.filePath1) Then
  713.             Throw New FileNotFoundException("Resource file not found.", Me.filePath1)
  714.             Exit Function
  715.         End If
  716.  
  717.         ' Read the ResX file.
  718.         Dim resX As ResXResourceReader = Nothing
  719.         Try
  720.             resX = New ResXResourceReader(Me.filePath1) With {.UseResXDataNodes = True}
  721.             Using resX
  722.  
  723.                 For Each entry As DictionaryEntry In resX
  724.  
  725.                     Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
  726.  
  727.                     If node.GetValue(DirectCast(Nothing, ITypeResolutionService)).GetType Is GetType(T) Then
  728.  
  729.                         Yield New Resource(Of T)(name:=node.Name,
  730.                                            data:=DirectCast(node.GetValue(DirectCast(Nothing, ITypeResolutionService)), T),
  731.                                            comment:=node.Comment)
  732.  
  733.                     End If
  734.  
  735.                 Next entry
  736.  
  737.             End Using ' resX
  738.  
  739.         Catch ex As Exception
  740.             Throw
  741.  
  742.         Finally
  743.             If resX IsNot Nothing Then
  744.                 resX.Close()
  745.             End If
  746.  
  747.         End Try
  748.  
  749.     End Function
  750.  
  751.     ''' <summary>
  752.     ''' Removes a resource by the specified name from the .Net managed resource file.
  753.     ''' </summary>
  754.     ''' <param name="name">The resource name.</param>
  755.     ''' <param name="stringComparison">The <see cref="StringComparison"/> to compare the resource name.</param>
  756.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  757.     ''' <exception cref="System.ArgumentException">Any resource found matching the specified name.;name</exception>
  758.     Public Sub RemoveResource(ByVal name As String,
  759.                               Optional ByVal stringComparison As StringComparison =
  760.                                              StringComparison.OrdinalIgnoreCase)
  761.  
  762.         If Not File.Exists(Me.filePath1) Then
  763.             Throw New FileNotFoundException("Resource file not found.", Me.filePath1)
  764.             Exit Sub
  765.         End If
  766.  
  767.         If Me.FindResource(name, stringComparison) Is Nothing Then
  768.             Throw New ArgumentException("Any resource found matching the specified name.", "name")
  769.             Exit Sub
  770.         End If
  771.  
  772.         Dim resources As New List(Of ResXDataNode)
  773.         Dim resX As ResXResourceReader = Nothing
  774.         Dim resXWritter As ResXResourceWriter = Nothing
  775.  
  776.         Try
  777.             resX = New ResXResourceReader(Me.filePath1) With {.UseResXDataNodes = True}
  778.             Using resX
  779.  
  780.                 For Each entry As DictionaryEntry In resX
  781.  
  782.                     If Not entry.Key.ToString.Equals(name, stringComparison) Then
  783.  
  784.                         Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
  785.                         resources.Add(New ResXDataNode(name:=node.Name, value:=node.GetValue(DirectCast(Nothing, ITypeResolutionService))) With {.Comment = node.Comment})
  786.  
  787.                     End If
  788.  
  789.                 Next entry
  790.  
  791.             End Using
  792.  
  793.             ' Add the resource in the ResX file.
  794.             ' Note: This will replace the current ResX file.
  795.             resXWritter = New ResXResourceWriter(Me.filePath1)
  796.             Using resXWritter
  797.  
  798.                 ' Add the retrieved resources into the ResX file.
  799.                 If resources IsNot Nothing Then
  800.                     For Each resourceItem As ResXDataNode In resources
  801.                         resXWritter.AddResource(resourceItem)
  802.                     Next resourceItem
  803.                 End If
  804.  
  805.                 resXWritter.Generate()
  806.  
  807.             End Using ' resXWritter
  808.  
  809.         Catch ex As Exception
  810.             Throw
  811.  
  812.         Finally
  813.             If resX IsNot Nothing Then
  814.                 resX.Close()
  815.             End If
  816.  
  817.             If resXWritter IsNot Nothing Then
  818.                 resXWritter.Close()
  819.             End If
  820.  
  821.             resources.Clear()
  822.  
  823.         End Try
  824.  
  825.     End Sub
  826.  
  827. #End Region
  828.  
  829. #Region " Private Methods "
  830.  
  831.     ''' <summary>
  832.     ''' Adds or replaces a resource into the .Net managed resource file.
  833.     ''' </summary>
  834.     ''' <param name="replace">if set to <c>true</c>, the resource will be replaced.</param>
  835.     ''' <param name="name">The resource name.</param>
  836.     ''' <param name="data">The resource data.</param>
  837.     ''' <param name="comment">The resource comment.</param>
  838.     ''' <exception cref="System.IO.FileNotFoundException">Resource file not found.</exception>
  839.     ''' <exception cref="System.ArgumentException">A resource with the same name already exists in the table.;name</exception>
  840.     Private Sub AddResource(ByVal replace As Boolean,
  841.                             ByVal name As String,
  842.                             ByVal data As Object,
  843.                             ByVal comment As String)
  844.  
  845.         If Not File.Exists(Me.filePath1) Then
  846.             Throw New FileNotFoundException("Resource file not found.", Me.filePath1)
  847.             Exit Sub
  848.         End If
  849.  
  850.         Dim resources As New List(Of ResXDataNode)
  851.         Dim resX As ResXResourceReader = Nothing
  852.         Dim resXWritter As ResXResourceWriter = Nothing
  853.  
  854.         Try
  855.             resX = New ResXResourceReader(Me.filePath1) With {.UseResXDataNodes = True}
  856.             Using resX
  857.  
  858.                 For Each entry As DictionaryEntry In resX
  859.  
  860.                     If Not replace AndAlso entry.Key.ToString.Equals(name, StringComparison.OrdinalIgnoreCase) Then
  861.                         Throw New ArgumentException("A resource with the same name already exists in the table.", "name")
  862.  
  863.                     Else
  864.                         Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
  865.                         resources.Add(New ResXDataNode(name:=node.Name, value:=node.GetValue(DirectCast(Nothing, ITypeResolutionService))) With {.Comment = node.Comment})
  866.  
  867.                     End If
  868.  
  869.                 Next entry
  870.  
  871.             End Using
  872.  
  873.             ' Add the resource in the ResX file.
  874.             ' Note: This will replace the current ResX file.
  875.             resXWritter = New ResXResourceWriter(Me.filePath1)
  876.             Using resXWritter
  877.  
  878.                 ' Add the retrieved resources into the ResX file.
  879.                 If resources IsNot Nothing Then
  880.                     For Each resourceItem As ResXDataNode In resources
  881.                         resXWritter.AddResource(resourceItem)
  882.                     Next resourceItem
  883.                 End If
  884.  
  885.                 ' Add the specified resource into the ResX file.
  886.                 resXWritter.AddResource(New ResXDataNode(name, data) With {.Name = name, .Comment = comment})
  887.                 resXWritter.Generate()
  888.  
  889.             End Using ' resXWritter
  890.  
  891.         Catch ex As Exception
  892.             Throw
  893.  
  894.         Finally
  895.             If resX IsNot Nothing Then
  896.                 resX.Close()
  897.             End If
  898.  
  899.             If resXWritter IsNot Nothing Then
  900.                 resXWritter.Close()
  901.             End If
  902.  
  903.             resources.Clear()
  904.  
  905.         End Try
  906.  
  907.     End Sub
  908.  
  909.     ''' <summary>
  910.     ''' Gets all the resources contained in the .Net managed resource file.
  911.     ''' </summary>
  912.     ''' <returns>IEnumerable(Of Resource).</returns>
  913.     Private Iterator Function GetResources() As IEnumerable(Of Resource)
  914.  
  915.         ' Read the ResX file.
  916.         Using resX As New Resources.ResXResourceReader(Me.filePath1) With {.UseResXDataNodes = True}
  917.  
  918.             For Each entry As DictionaryEntry In resX
  919.  
  920.                 Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
  921.  
  922.                 Yield New Resource(name:=node.Name,
  923.                                    data:=node.GetValue(DirectCast(Nothing, ITypeResolutionService)),
  924.                                    comment:=node.Comment)
  925.  
  926.             Next entry
  927.  
  928.         End Using ' resX
  929.  
  930.     End Function
  931.  
  932. #End Region
  933.  
  934. #Region " Hidden Methods "
  935.  
  936.     ''' <summary>
  937.     ''' Determines whether the specified System.Object instances are considered equal.
  938.     ''' </summary>
  939.     <EditorBrowsable(EditorBrowsableState.Never)>
  940.     Public Shadows Function Equals(ByVal obj As Object) As Boolean
  941.         Return MyBase.Equals(obj)
  942.     End Function
  943.  
  944.     ''' <summary>
  945.     ''' Serves as a hash function for a particular type.
  946.     ''' </summary>
  947.     <EditorBrowsable(EditorBrowsableState.Never)>
  948.     Public Shadows Function GetHashCode() As Integer
  949.         Return MyBase.GetHashCode
  950.     End Function
  951.  
  952.     ''' <summary>
  953.     ''' Gets the System.Type of the current instance.
  954.     ''' </summary>
  955.     ''' <returns>The exact runtime type of the current instance.</returns>
  956.     <EditorBrowsable(EditorBrowsableState.Never)>
  957.     Public Shadows Function [GetType]() As Type
  958.         Return MyBase.GetType
  959.     End Function
  960.  
  961.     ''' <summary>
  962.     ''' Returns a String that represents the current object.
  963.     ''' </summary>
  964.     <EditorBrowsable(EditorBrowsableState.Never)>
  965.     Public Shadows Function ToString() As String
  966.         Return MyBase.ToString
  967.     End Function
  968.  
  969. #End Region
  970.  
  971. End Class
  972.  
  973. ]]></Code>
  974.     </Snippet>
  975.   </CodeSnippet>
  976. </CodeSnippets>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement