2014-05-03 29 views
-1

我想寫一個方法,應該將資源添加到ResX文件中,資源被添加但ResX文件中包含的其他資源丟失,我認爲該文件是隻用我添加的資源替換一個新的。ResXResourceWriter方法替換整個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

看起來你只是使用的ResX作家在一個文件名。我認爲你需要使用ResXResourceSet,加載現有的資源,從集合中獲得一個編寫者,使用編寫者添加你的​​新條目,生成新的資源。 –

+0

@Marvin Smit感謝您的評論,你的意思是?:1.檢索文件中的所有現有資源,2.再次將它們全部添加到文件中(這應該替換現有文件,但是我將所有檢索到的來自原始文件的資源)以及最後3.在該文件中添加新的資源?我已經承認這些課程會促進這項任務。 – ElektroStudios

+0

1:是的,將它們讀入資源集。 2:不,不需要複製。 3:從資源集實例中獲取作者。然後使用你提到的代碼。 –

回答

0

最後,這是我在做什麼(檢索存在的資源重新指定文件中一起用新的複製它們資源),但我不認爲這是一個一致的解決方案,所以I would like to mark as accepted a solution that avoids the copying of the original resources,我真的假設一個提供的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