How to create .resources file and write string values using vb.net

You can store strings values in the .resources file at run time by creating .resources file through ResourceWriter class. you can save objects only in .resources and .resx resource files.

The following example demonstrate how we are using ResourceWriter class for writing three values in the .resources file. In the following code first we create the resources file with unique name and then add string values one by one with the help of AddResources() function. see this:

Dim rWriter As IResourceWriter
rWriter = New ResourceWriter("strMyApp.resources")
rWriter.AddResource("First_Name", "Author")
rWriter.AddResource("Last_Name", "Code")
rWriter.AddResource("Age", "25")
rWriter.Close()

After execute code strMyApp.resources file will be created in the application execution path.

And now in the below section we will see that how we can read these values. For it we are using ResourceReader class.

Dim reader As New ResourceReader("strMyApp.resources")
        Dim dEnum As IDictionaryEnumerator = reader.GetEnumerator()
 
        Dim Firstname As String = ""
        Dim Lastname As String = ""
        Dim Age As String = ""
        While dEnum.MoveNext()
            Select Case dEnum.Key
                Case "First_Name"
                    Firstname = dEnum.Value
                Case "Last_Name"
                    Lastname = dEnum.Value
                Case "Age"
                    Age = dEnum.Value
            End Select
         End While
        reader.Close()

I hope all the above code sample will help you…
Thanks

One thought on “How to create .resources file and write string values using vb.net”

  1. Hi Hirendra,

    Your code works well in WSP. But when I run it with http://localhost/SiteName it gives error file not found. Could not find file ‘c:windowssystem32inetsrvstrMyApp.resources’.
    Please help me sort it out.

Comments are closed.