1

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.

Benjli
  • 125
  • 2
  • 13

1 Answers1

1

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.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Thank you. And how can I load a .resources file to my.resources? – Benjli Sep 25 '13 at 13:40
  • Typically resources are compiled into the assembly. Are you saying that you will be loading the resources dynamically at run-time from a `.resources` file? If so, that changes things considerably. – Steven Doggart Sep 25 '13 at 13:47
  • Yes, I want to load them at runtime if that's possiable, and then I will be able to delete or add items from the .resources file and then restart the program. – Benjli Sep 25 '13 at 13:53
  • That worked - thank you! I have only one question - when I try to save the resources, the following exception appears: "The process cannot access the file 'XYZ' because it is being used by another process." How can I save my resources without getting this exception? – Benjli Sep 26 '13 at 17:18
  • Well, you won't be able to do it if the file is open. First you'd need to determine who has the file open. Is it your own process, or some other process that has it open and why? When using the `ResourceReader`, the file should be closed when the `End Using` line is executed. – Steven Doggart Sep 26 '13 at 17:21
  • I sloved the problem - it was an IO operation with the file. Thank you again. – Benjli Sep 26 '13 at 17:28