Guest User

ResXManager.vb

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