我正在尝试编写一个应该向ResX文件添加资源的方法,添加了资源但是ResX文件中包含的其他资源都丢失了,我认为该文件被一个只有新文件的文件所取代我添加的资源。
PS:我从HERE获得了这个例子。
我做错了什么,我需要做哪些改变才能解决这个问题?
''' <summary>
''' Adds a resource inside a ResX resource table.
''' </summary>
''' <param name="ResXFile">Indicates the ResX file to add the resource.</param>
''' <exception cref="System.Exception"></exception>
Private Sub AddResXResource(ByVal ResXFile As String,
ByVal ResourceName As String,
ByVal Resource As Object,
Optional ByVal Comment As String = Nothing)
If Not IO.File.Exists(ResXFile) Then
Throw New Exception(String.Format("Resource file: '{0}' not found.", ResXFile))
Else
' Open the existent ResX file.
Using ResXWritter As New Resources.ResXResourceWriter(ResXFile)
ResXWritter.AddResource(New Resources.ResXDataNode(ResourceName, Resource) _
With {.Name = ResourceName, .Comment = Comment})
ResXWritter.Generate()
End Using ' ResXWritter As New Resources.ResXResourceWriter(ResXFile)
End If ' Not IO.File.Exists(ResXFile)
End Sub
这就是我使用该方法的方法:
Dim MyResource As Bitmap = SystemIcons.Information.ToBitmap
AddResXResource(".\Resources.resx", "SysIcon_Info", MyResource, "Resource comment")
答案 0 :(得分:0)
最后这就是我正在做的事情(检索现有的资源,将它们与新的资源一起在指定的文件中再次复制),但我不认为这是一致的解决方案所以I would like to mark as accepted a solution that avoids the copying of the original resources
,I实际上,假设其中一个提供的ResX类/方法应该完成该任务。
代码:
' Add ResX Resource
' ( By Elektro )
'
' Usage Examples:
' AddResXResource(".\Resources.resx", "Bitmap Resource", SystemIcons.Information.ToBitmap, "Resource Comment")
'
''' <summary>
''' Adds a resource inside a ResX resource table.
''' </summary>
''' <param name="ResXFile">Indicates the ResX file to add the resource.</param>
''' <exception cref="System.Exception"></exception>
Private Sub AddResXResource(ByVal ResXFile As String,
ByVal ResourceName As String,
ByVal Resource As Object,
Optional ByVal Comment As String = Nothing)
Dim [Resources] As New Dictionary(Of String, Object)
If Not IO.File.Exists(ResXFile) Then
Throw New Exception(String.Format("Resource file: '{0}' not found.", ResXFile))
Else
' Open the existent ResX file.
Dim ResX = New Resources.ResXResourceSet(ResXFile)
' Get the resource enumerator.
Dim ResXDictionay As IDictionaryEnumerator = ResX.GetEnumerator()
' Loop through the existing resources to copy them in the collection.
Do While ResXDictionay.MoveNext()
Resources.Add(CStr(ResXDictionay.Key), ResXDictionay.Value)
Loop ' ResXDictionay.MoveNext()
' Add the resource(s) in the ResX file.
' Note: This will replace the existent file.
Using ResXWritter As New Resources.ResXResourceWriter(ResXFile)
' Add the retrieved resources into the ResX file.
If [Resources] IsNot Nothing Then
For Each ResourceItem In [Resources]
ResXWritter.AddResource(ResourceItem.Key, ResourceItem.Value)
Next ResourceItem
End If
' Add the specified resource into the ResX file.
ResXWritter.AddResource(New Resources.ResXDataNode(ResourceName, Resource) _
With {.Name = ResourceName, .Comment = Comment})
ResXWritter.Generate()
End Using ' ResXWritter As New Resources.ResXResourceWriter(ResXFile)
End If ' Not IO.File.Exists(ResXFile)
[Resources] = Nothing
End Sub