Question

I am writing application that use resources, and I have some questions about it:

  1. Can app-inside resources (my.resources) be edited, removed and added, and how can I do it?
  2. How can I create an array of the app-inside resources?
  3. How can I read an .resources file and turn it into an array of the items inside it?

Thank you very much.

Était-ce utile?

La solution

Embedded resources cannot be changed at run-time. You can access them as a collection. For instance:

' Get resource value by string name
Dim value1 As Object = My.Resources.ResourceManager.GetObject("name1")
Dim value2 As String = My.Resources.ResourceManager.GetString("name2")

' Loop through list of resources
Dim rset As ResourceSet = My.Resources.ResourceManager.GetResourceSet(Thread.CurrentThread.CurrentUICulture, True, True)
For Each i As DictionaryEntry In rset
    Dim name As String = i.Key
    Dim value As Object = i.Value
Next

If you want to, at run-time, read and modify resources which are stored in an external .resources file, the .NET framework provides some classes in the System.Resources namespace which you can use. The ResourceReader class allows you to read a .resources file, like this:

Using reader As New ResourceReader("test.resources")
    For Each i As DictionaryEntry In reader
        Dim name As String = i.Key
        Dim value As Object = i.Value
    Next
End Using

And you can use the ResourceWriter class to create a new .resources file, like this:

Using writer As New ResourceWriter("test.resources")
    writer.AddResource("name1", value1)
    writer.AddResource("name2", value2)
End Using

However, the real question to ask is, do you have to use .resources files? Typically .resource files are only used as a assembly-building-step so that they can be embedded in the assembly. If you want to store data in external files, typically you would choose to store them in some other format. .NET provides many object serialization options which make it easy to store objects in XML, text, binary, or other types of files as well as databases.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top