Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Modified : 15-November-2015
- ' ***********************************************************************
- #Region " Public Members Summary "
- #Region " Constructors "
- ' ResXManager.New(String)
- #End Region
- #Region " Properties "
- ' ResXManager.FilePath As String
- ' ResXManager.Resources As IEnumerable(Of Resource)
- #End Region
- #Region " Functions "
- ' ResXManager.FindResource(Of T)(String, Opt: StringComparison) As Resource(Of T)
- ' ResXManager.FindResource(String, Opt: StringComparison) As Resource
- ' ResXManager.FindResources(Of T)() As IEnumerable(Of Resource(Of T))
- #End Region
- #Region " Methods "
- ' ResXManager.Create(Opt: Boolean)
- ' ResXManager.AddResource(String, Object, Opt: String)
- ' ResXManager.AddResource(Of T)(String, T, Opt: String)
- ' ResXManager.AddResource(Of T)(Resource(Of T))
- ' ResXManager.ReplaceResource(String, Object, Opt: String)
- ' ResXManager.ReplaceResource(Of T)(String, T, Opt: String)
- ' ResXManager.ReplaceResource(Of T)(Resource(Of T))
- ' ResXManager.RemoveResource(String, Opt: StringComparison)
- #End Region
- #End Region
- #Region " Usage Examples "
- 'Imports System.IO
- 'Imports System.Text
- 'Public Class Form1
- ' Dim resX As New ResXManager(Path.Combine(Application.StartupPath, "MyResources.ResX"))
- ' With resX
- ' ' Create or overwrite the ResX file.
- ' .Create(overwrite:=True)
- ' ' Add a string resource.
- ' Dim resStr As New Resource(Of String)("String Resource", "Hello World!", "String Comment")
- ' .AddResource(Of String)(resStr)
- ' ' Add a bitmap resource.
- ' Dim resBmp As New Resource(Of Bitmap)("Bitmap Resource", SystemIcons.Information.ToBitmap, "Bitmap Comment")
- ' .AddResource(Of Bitmap)(resBmp)
- ' ' Add a binary resource.
- ' Dim resBin As New Resource(Of Byte())("Binary Resource", File.ReadAllBytes("C:\File.mp3"), "Binary Comment")
- ' .AddResource(Of Byte())(resBin)
- ' End With
- ' ' *******************************************************************************************************
- ' ' Get the string resource.
- ' Dim stringResource As Resource(Of String) =
- ' resX.FindResource(Of String)("String Resource", StringComparison.OrdinalIgnoreCase)
- ' ' Get the bitmap resource.
- ' Dim bitmapResource As Resource(Of Bitmap) =
- ' resX.FindResource(Of Bitmap)("Bitmap Resource", StringComparison.OrdinalIgnoreCase)
- ' ' Get the binary resource.
- ' Dim binaryResource As Resource(Of Byte()) =
- ' resX.FindResource(Of Byte())("Binary Resource", StringComparison.OrdinalIgnoreCase)
- ' ' *******************************************************************************************************
- ' ' Get the string data.
- ' Dim stringData As String = stringResource.Data
- ' ' Get the bitmap data.
- ' Dim bitmapData As Bitmap = bitmapResource.Data
- ' ' Get the binary data.
- ' Dim binaryData As Byte() = binaryResource.Data
- ' ' *******************************************************************************************************
- ' ' Get all the resources at once.
- ' Dim resources As IEnumerable(Of Resource) = resX.Resources
- ' ' Get all the resources of specific Type at once.
- ' Dim stringResources As IEnumerable(Of Resource(Of String)) = resX.FindResources(Of String)()
- ' ' *******************************************************************************************************
- ' ' Get all the resource datas at once from Resource collection.
- ' Dim resourceDatas As IEnumerable(Of Object) =
- ' From res As Resource In resX.Resources
- ' Select res.Data
- ' ' Get all the resource datas of specific Type at once from Resource collection.
- ' Dim stringResourceDatas As IEnumerable(Of String) =
- ' From res As Resource In resX.Resources
- ' Where res.Type Is GetType(String)
- ' Select DirectCast(res.Data, String)
- ' ' *******************************************************************************************************
- ' ' Treat the string data as you like.
- ' MessageBox.Show(stringData, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
- ' ' Treat the bitmap data as you like.
- ' Me.Icon = Icon.FromHandle(bitmapData.GetHicon)
- ' ' Treat the binary data as you like.
- ' File.WriteAllBytes("C:\new file.mp3", binaryData)
- ' ' *******************************************************************************************************
- ' ' Iterate all the resources.
- ' For Each res As Resource In resX.Resources
- ' Dim sb As New StringBuilder
- ' sb.AppendLine(String.Format("Name...: {0}", res.Name))
- ' sb.AppendLine(String.Format("Comment: {0}", res.Comment))
- ' sb.AppendLine(String.Format("Type...: {0}", res.Data.GetType().FullName))
- ' sb.AppendLine(String.Format("Data...: {0}", res.Data.ToString()))
- ' MsgBox(sb.ToString())
- ' Next res
- ' ' Iterate all the resources of specific Type.
- ' For Each res As Resource(Of String) In resX.FindResources(Of String)()
- ' Dim sb As New StringBuilder
- ' sb.AppendLine(String.Format("Name...: {0}", res.Name))
- ' sb.AppendLine(String.Format("Comment: {0}", res.Comment))
- ' sb.AppendLine(String.Format("Type...: {0}", res.Type.ToString()))
- ' sb.AppendLine(String.Format("Data...: {0}", res.Data.ToString()))
- ' MsgBox(sb.ToString())
- ' Next res
- ' ' *******************************************************************************************************
- ' ' Remove a resource.
- ' resX.RemoveResource("Binary Resource")
- ' ' GC.Collect()
- 'End Sub
- 'End Class
- #End Region
- #Region " Option Statements "
- Option Strict On
- Option Explicit On
- Option Infer Off
- #End Region
- #Region " Imports "
- Imports System.ComponentModel
- Imports System.ComponentModel.Design
- Imports System.IO
- Imports System.Resources
- Imports Elektro.Core.Types
- #End Region
- #Region " ResX Manager "
- Namespace Resources.Types
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Manages a .Net managed resource (ResX) file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Dim resX As New ResXManager(Path.Combine(Application.StartupPath, "MyResources.ResX"))
- ''' With resX
- '''
- ''' ' Create or overwrite the ResX file.
- ''' .Create(overwrite:=True)
- '''
- ''' ' Add a string resource.
- ''' Dim resStr As New Resource(Of String)("String Resource", "Hello World!", "String Comment")
- ''' .AddResource(Of String)(resStr)
- '''
- ''' ' Add a bitmap resource.
- ''' Dim resBmp As New Resource(Of Bitmap)("Bitmap Resource", SystemIcons.Information.ToBitmap, "Bitmap Comment")
- ''' .AddResource(Of Bitmap)(resBmp)
- '''
- ''' ' Add a binary resource.
- ''' Dim resBin As New Resource(Of Byte())("Binary Resource", File.ReadAllBytes("C:\File.mp3"), "Binary Comment")
- ''' .AddResource(Of Byte())(resBin)
- '''
- ''' End With
- '''
- ''' ' *******************************************************************************************************
- '''
- ''' ' Get the string resource.
- ''' Dim stringResource As Resource(Of String) =
- ''' resX.FindResource(Of String)("String Resource", StringComparison.OrdinalIgnoreCase)
- '''
- ''' ' Get the bitmap resource.
- ''' Dim bitmapResource As Resource(Of Bitmap) =
- ''' resX.FindResource(Of Bitmap)("Bitmap Resource", StringComparison.OrdinalIgnoreCase)
- '''
- ''' ' Get the binary resource.
- ''' Dim binaryResource As Resource(Of Byte()) =
- ''' resX.FindResource(Of Byte())("Binary Resource", StringComparison.OrdinalIgnoreCase)
- '''
- ''' ' *******************************************************************************************************
- '''
- ''' ' Get the string data.
- ''' Dim stringData As String = stringResource.Data
- '''
- ''' ' Get the bitmap data.
- ''' Dim bitmapData As Bitmap = bitmapResource.Data
- '''
- ''' ' Get the binary data.
- ''' Dim binaryData As Byte() = binaryResource.Data
- '''
- ''' ' *******************************************************************************************************
- '''
- ''' ' Get all the resources at once.
- ''' Dim resources As IEnumerable(Of Resource) = resX.Resources
- '''
- ''' ' Get all the resources of specific Type at once.
- ''' Dim stringResources As IEnumerable(Of Resource(Of String)) = resX.FindResources(Of String)()
- '''
- ''' ' *******************************************************************************************************
- '''
- ''' ' Get all the resource datas at once from Resource collection.
- ''' Dim resourceDatas As IEnumerable(Of Object) =
- ''' From res As Resource In resX.Resources
- ''' Select res.Data
- '''
- ''' ' Get all the resource datas of specific Type at once from Resource collection.
- ''' Dim stringResourceDatas As IEnumerable(Of String) =
- ''' From res As Resource In resX.Resources
- ''' Where res.Type Is GetType(String)
- ''' Select DirectCast(res.Data, String)
- '''
- ''' ' *******************************************************************************************************
- '''
- ''' ' Treat the string data as you like.
- ''' MessageBox.Show(stringData, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
- '''
- ''' ' Treat the bitmap data as you like.
- ''' Me.Icon = Icon.FromHandle(bitmapData.GetHicon)
- '''
- ''' ' Treat the binary data as you like.
- ''' File.WriteAllBytes("C:\new file.mp3", binaryData)
- '''
- ''' ' *******************************************************************************************************
- '''
- ''' ' Iterate all the resources.
- ''' For Each res As Resource In resX.Resources
- '''
- ''' Dim sb As New StringBuilder
- '''
- ''' sb.AppendLine(String.Format("Name...: {0}", res.Name))
- ''' sb.AppendLine(String.Format("Comment: {0}", res.Comment))
- ''' sb.AppendLine(String.Format("Type...: {0}", res.Data.GetType().FullName))
- ''' sb.AppendLine(String.Format("Data...: {0}", res.Data.ToString()))
- '''
- ''' MsgBox(sb.ToString())
- '''
- ''' Next res
- '''
- ''' ' Iterate all the resources of specific Type.
- ''' For Each res As Resource(Of String) In resX.FindResources(Of String)()
- '''
- ''' Dim sb As New StringBuilder
- '''
- ''' sb.AppendLine(String.Format("Name...: {0}", res.Name))
- ''' sb.AppendLine(String.Format("Comment: {0}", res.Comment))
- ''' sb.AppendLine(String.Format("Type...: {0}", res.Type.ToString()))
- ''' sb.AppendLine(String.Format("Data...: {0}", res.Data.ToString()))
- '''
- ''' MsgBox(sb.ToString())
- '''
- ''' Next res
- '''
- ''' ' *******************************************************************************************************
- '''
- ''' ' Remove a resource.
- ''' resX.RemoveResource("Binary Resource")
- '''
- ''' ' GC.Collect()
- '''
- ''' End Sub
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- <ImmutableObject(True)>
- Public Class ResXManager : Inherits AestheticObject
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the .Net managed resource file path.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The .Net managed resource filepath.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable ReadOnly Property FilePath As String
- <DebuggerStepThrough>
- Get
- Return Me.filepathB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing field )
- ''' The .Net managed resource file path.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected ReadOnly filepathB As String
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the resources contained in the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The resources.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable ReadOnly Property Resources As IEnumerable(Of Resource)
- <DebuggerStepThrough>
- Get
- Return Me.GetResources()
- End Get
- End Property
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Prevents a default instance of the <see cref="ResXManager"/> class from being created.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerNonUserCode>
- Private Sub New()
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="ResXManager"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="resxFilePath">
- ''' The .Net managed resource filepath.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Sub New(ByVal resxFilePath As String)
- Me.filepathB = resxFilePath
- End Sub
- #End Region
- #Region " Public Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Creates the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="overwrite">
- ''' If set to <see langword="True"/>, overwrites any existent file.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.IOException">
- ''' Resource file already exists.
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Sub Create(Optional ByVal overwrite As Boolean = False)
- If Not overwrite AndAlso File.Exists(Me.filepathB) Then
- Throw New IOException(String.Format("Resource file already exists: {0}", Me.filepathB))
- Else
- Using resXWritter As New ResXResourceWriter(Me.filepathB)
- resXWritter.Generate()
- End Using
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds a resource into the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <typeparam name="T">
- ''' The type.
- ''' </typeparam>
- '''
- ''' <param name="resource">
- ''' The resource.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' A resource with the same name already exists in the table.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Sub AddResource(Of T)(ByVal resource As Resource(Of T))
- Me.AddResource(replace:=False, name:=resource.Name, data:=resource.Data, comment:=resource.Comment)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds a resource into the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="data">
- ''' The resource data.
- ''' </param>
- '''
- ''' <param name="comment">
- ''' The resource comment.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' A resource with the same name already exists in the table.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Sub AddResource(ByVal name As String,
- ByVal data As Object,
- Optional ByVal comment As String = "")
- Me.AddResource(replace:=False, name:=name, data:=data, comment:=comment)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds a specified resource of the specified type into the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <typeparam name="T">
- ''' The type.
- ''' </typeparam>
- '''
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="data">
- ''' The resource data.
- ''' </param>
- '''
- ''' <param name="comment">
- ''' The resource comment.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' A resource with the same name already exists in the table.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Sub AddResource(Of T)(ByVal name As String,
- ByVal data As T,
- Optional ByVal comment As String = "")
- Me.AddResource(replace:=False, name:=name, data:=data, comment:=comment)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Replaces a resource by the specified name inside the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <typeparam name="T">
- ''' The type.
- ''' </typeparam>
- '''
- ''' <param name="resource">
- ''' The resource.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' A resource with the same name already exists in the table.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable Sub ReplaceResource(Of T)(ByVal resource As Resource(Of T))
- Me.AddResource(replace:=True, name:=resource.Name, data:=resource.Data, comment:=resource.Comment)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Replaces a resource by the specified name inside the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="data">
- ''' The resource data.
- ''' </param>
- '''
- ''' <param name="comment">
- ''' The resource comment.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' A resource with the same name already exists in the table.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Sub ReplaceResource(ByVal name As String,
- ByVal data As Object,
- Optional ByVal comment As String = "")
- Me.AddResource(replace:=True, name:=name, data:=data, comment:=comment)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Replaces a resource by the specified name of the specified type inside the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <typeparam name="T">
- ''' The type.
- ''' </typeparam>
- '''
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="data">
- ''' The resource data.
- ''' </param>
- '''
- ''' <param name="comment">
- ''' The resource comment.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' A resource with the same name already exists in the table.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Sub ReplaceResource(Of T)(ByVal name As String,
- ByVal data As T,
- Optional ByVal comment As String = "")
- Me.AddResource(replace:=True, name:=name, data:=data, comment:=comment)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Finds a resource by the specified name of specified type inside the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <typeparam name="T">
- ''' The type.
- ''' </typeparam>
- '''
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="stringComparison">
- ''' The <see cref="StringComparison"/> to compare the resource name.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The resource.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' Resource with the specified name is not found.;name
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' The specified Type differs from the resource Type.;T
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Function FindResource(Of T)(ByVal name As String,
- Optional ByVal stringComparison As StringComparison =
- Global.System.StringComparison.OrdinalIgnoreCase) As Resource(Of T)
- If Not File.Exists(Me.filepathB) Then
- Throw New FileNotFoundException("Resource file not found.", Me.filepathB)
- Exit Function
- End If
- ' Read the ResX file.
- Dim res As Resource(Of T) = Nothing
- Try
- Using resX As New ResXResourceReader(Me.filepathB) With {.UseResXDataNodes = True}
- For Each entry As DictionaryEntry In resX
- If entry.Key.ToString().Equals(name, stringComparison) Then
- Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
- res = New Resource(Of T)(name:=node.Name,
- data:=DirectCast(node.GetValue(DirectCast(Nothing, ITypeResolutionService)), T),
- comment:=node.Comment)
- Exit For
- End If
- Next entry
- End Using ' resX
- Return res
- Catch ex As Exception
- Throw
- End Try
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Finds a resource by the specified name inside the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="stringComparison">
- ''' The <see cref="StringComparison"/> to compare the resource name.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The resource.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' Resource with the specified name is not found.;name
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' The specified Type differs from the resource Type.;T
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Function FindResource(ByVal name As String,
- Optional ByVal stringComparison As StringComparison =
- Global.System.StringComparison.OrdinalIgnoreCase) As Resource
- If Not File.Exists(Me.filepathB) Then
- Throw New FileNotFoundException("Resource file not found.", Me.filepathB)
- Exit Function
- End If
- ' Read the ResX file.
- Dim res As Resource = Nothing
- Try
- Using resX As New ResXResourceReader(Me.filepathB) With {.UseResXDataNodes = True}
- For Each entry As DictionaryEntry In resX
- If entry.Key.ToString().Equals(name, stringComparison) Then
- Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
- res = New Resource(name:=node.Name,
- data:=node.GetValue(DirectCast(Nothing, ITypeResolutionService)),
- comment:=node.Comment)
- Exit For
- End If
- Next entry
- End Using ' resX
- Return res
- Catch ex As Exception
- Throw
- End Try
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Finds the resources of the specified type inside the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <typeparam name="T">
- ''' The type.
- ''' </typeparam>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The resource.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' Resource with the specified name is not found.;name
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' The specified Type differs from the resource Type.;T
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Iterator Function FindResources(Of T)() As IEnumerable(Of Resource(Of T))
- If Not File.Exists(Me.filepathB) Then
- Throw New FileNotFoundException("Resource file not found.", Me.filepathB)
- Exit Function
- End If
- ' Read the ResX file.
- Try
- Using resX As New ResXResourceReader(Me.filepathB) With {.UseResXDataNodes = True}
- For Each entry As DictionaryEntry In resX
- Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
- If node.GetValue(DirectCast(Nothing, ITypeResolutionService)).GetType Is GetType(T) Then
- Yield New Resource(Of T)(name:=node.Name,
- data:=DirectCast(node.GetValue(DirectCast(Nothing, ITypeResolutionService)), T),
- comment:=node.Comment)
- End If
- Next entry
- End Using ' resX
- Catch ex As Exception
- Throw
- End Try
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Removes a resource by the specified name from the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="stringComparison">
- ''' The <see cref="StringComparison"/> to compare the resource name.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' Any resource found matching the specified name.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Overridable Sub RemoveResource(ByVal name As String,
- Optional ByVal stringComparison As StringComparison =
- Global.System.StringComparison.OrdinalIgnoreCase)
- If Not File.Exists(Me.filepathB) Then
- Throw New FileNotFoundException("Resource file not found.", Me.filepathB)
- Exit Sub
- End If
- If Me.FindResource(name, stringComparison) Is Nothing Then
- Throw New ArgumentException("Any resource found matching the specified name.", "name")
- Exit Sub
- End If
- Dim resources As New List(Of ResXDataNode)
- Try
- Using resX As New ResXResourceReader(Me.filepathB) With {.UseResXDataNodes = True}
- For Each entry As DictionaryEntry In resX
- If Not entry.Key.ToString().Equals(name, stringComparison) Then
- Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
- resources.Add(New ResXDataNode(name:=node.Name, value:=node.GetValue(DirectCast(Nothing, ITypeResolutionService))) With {.Comment = node.Comment})
- End If
- Next entry
- End Using
- ' Add the resource in the ResX file.
- ' Note: This will replace the current ResX file.
- Using resXWritter As New ResXResourceWriter(Me.filepathB)
- ' Add the retrieved resources into the ResX file.
- If resources IsNot Nothing Then
- For Each resourceItem As ResXDataNode In resources
- resXWritter.AddResource(resourceItem)
- Next resourceItem
- End If
- resXWritter.Generate()
- End Using ' resXWritter
- Catch ex As Exception
- Throw
- Finally
- resources.Clear()
- End Try
- End Sub
- #End Region
- #Region " Private Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Adds or replaces a resource into the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="replace">
- ''' If set to <see langword="True"/>, the resource will be replaced.
- ''' </param>
- '''
- ''' <param name="name">
- ''' The resource name.
- ''' </param>
- '''
- ''' <param name="data">
- ''' The resource data.
- ''' </param>
- '''
- ''' <param name="comment">
- ''' The resource comment.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Global.System.IO.FileNotFoundException">
- ''' Resource file not found.
- ''' </exception>
- '''
- ''' <exception cref="Global.System.ArgumentException">
- ''' A resource with the same name already exists in the table.;name
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Protected Overridable Sub AddResource(ByVal replace As Boolean,
- ByVal name As String,
- ByVal data As Object,
- ByVal comment As String)
- If Not File.Exists(Me.filepathB) Then
- Throw New FileNotFoundException("Resource file not found.", Me.filepathB)
- Exit Sub
- End If
- Dim resources As New List(Of ResXDataNode)
- Try
- Using resX As New ResXResourceReader(Me.filepathB) With {.UseResXDataNodes = True}
- For Each entry As DictionaryEntry In resX
- If Not replace AndAlso entry.Key.ToString().Equals(name, StringComparison.OrdinalIgnoreCase) Then
- Throw New ArgumentException("A resource with the same name already exists in the table.", "name")
- Else
- Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
- resources.Add(New ResXDataNode(name:=node.Name, value:=node.GetValue(DirectCast(Nothing, ITypeResolutionService))) With {.Comment = node.Comment})
- End If
- Next entry
- End Using
- ' Add the resource in the ResX file.
- ' Note: This will replace the current ResX file.
- Using resXWritter As New ResXResourceWriter(Me.filepathB)
- ' Add the retrieved resources into the ResX file.
- If resources IsNot Nothing Then
- For Each resourceItem As ResXDataNode In resources
- resXWritter.AddResource(resourceItem)
- Next resourceItem
- End If
- ' Add the specified resource into the ResX file.
- resXWritter.AddResource(New ResXDataNode(name, data) With {.Name = name, .Comment = comment})
- resXWritter.Generate()
- End Using ' resXWritter
- Catch ex As Exception
- Throw
- Finally
- resources.Clear()
- End Try
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets all the resources contained in the .Net managed resource file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see cref="IEnumerable(Of Resource)"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Protected Overridable Iterator Function GetResources() As IEnumerable(Of Resource)
- ' Read the ResX file.
- Using resX As New ResXResourceReader(Me.filepathB) With {.UseResXDataNodes = True}
- For Each entry As DictionaryEntry In resX
- Dim node As ResXDataNode = CType(entry.Value, ResXDataNode)
- Yield New Resource(name:=node.Name,
- data:=node.GetValue(DirectCast(Nothing, ITypeResolutionService)),
- comment:=node.Comment)
- Next entry
- End Using ' resX
- End Function
- #End Region
- End Class
- End Namespace
- #End Region
Add Comment
Please, Sign In to add comment