Results 1 to 1 of 1

Thread: Simple Write-Read External Resource File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2016
    Location
    Sabattus, ME
    Posts
    87

    Simple Write-Read External Resource File

    Here is a simple way to create and read in your own external resource file (not using the resources panel in VS).
    This method will allow you to store all your settings and external files packed into ONE file.
    This is just a basic example... you will have to edit the parts you need.

    You can save strings, images and binary (not in this example)
    You can even save rtf files if they have images in the rtf file itself.

    This example uses two external files (image.png & text.rtf) not included in the example.
    So, you will have to supply your own image and rtf file.

    You will have to add your own error handling

    I use the file name "items.resources", but you can rename it to whatever you want (ex. mycontent.myext)

    Name:  resourcescreen.jpg
Views: 1247
Size:  25.3 KB

    To use...

    1) Create a form
    2) Add button1 to write data
    3) Add button2 to read data
    4) Add a Label to show string
    5) Add Picture box
    6) Add RichTextBox
    7) Copy and paste below code in code view.

    VB Code:

    Code:
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            ' Create a file stream to encapsulate items.resources.
            Dim fs As New FileStream("items.resources", FileMode.OpenOrCreate, FileAccess.Write)
    
            ' set variables
            Dim writer = New System.Resources.ResourceWriter(fs)
            Dim Bitmap = New Bitmap("image.png")
            Dim rtf As String = File.ReadAllText("text.rtf")
            
           'write data to file
            writer.AddResource("StringX", "My Custom String of Text")
            writer.AddResource("imgX", Bitmap)
            writer.AddResource("fileX", rtf)
    
            ' Write the resources to the stream, and close it.
            writer.Close()
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            Dim res As New Resources.ResourceReader("items.resources")
            Dim dict As IDictionaryEnumerator = res.GetEnumerator()
    
            'This is the format of the array values
            '{0} & {1} are the field place holders
            'dict.key is the value of {0}
            'dict.value is the value of {1}
            'Console.WriteLine("{0}, {1}", dict.Key, dict.Value)
            
            Do While dict.MoveNext()
                If dict.Key = "StringX" Then
                    Label1.Text = dict.Value
                End If
                If dict.Key = "imgX" Then
                    PictureBox1.Image = dict.Value
                End If
                If dict.Key = "fileX" Then
                    RichTextBox1.Rtf = dict.Value
                End If
            Loop
    
            res.Close()
        End Sub
    
    End Class
    What you could do with this is limitless.
    One example... coupled with another EXE template (using copy and rename functions to simulate "creating an exe"), you could allow a user to create their own app and include the resource file with their EXE file. All the user's data would be packed away in one resource file.

    Enjoy
    Last edited by technipixel; Aug 2nd, 2016 at 09:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width