Results 1 to 3 of 3

Thread: Resource Files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2016
    Location
    Anderson, SC
    Posts
    70

    Resource Files

    I can't quite wrap my mind around Resource Files. I can put images and .mp4 files in the Resource but not sure how to use them, i.e. access them in the execution of the project. And, if they are in the Resource will they be available to the .exe when it is put on another computer, i.e. I don't have to put those files on the other computer?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jan 2016
    Location
    Anderson, SC
    Posts
    70

    Re: Resource Files

    What I mean is a graphic, e.g. pic.jpg, is in the Resource how do I use if if I want to place it in a picture1.image = ? Or to place a .mp4 file in the Media Player object?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Resource Files

    There are no resource files at run time. That's the whole point. When you add a resource, its contents gets compiled into the EXE. It goes wherever the EXE goes and cannot be changed by the user. You see the original file in your project because it's a source file, just like your VB code files. When you build, the code and the resources get compiled and output as a single EXE.

    There are two ways to add resources in a VB.NET project. Unless you have a very good reason for doing otherwise, you should be adding them via the Resources page of the project properties. If you haven't done that, delete the files from your project and start again.

    When you add a file named Something.ext on the Resources page, VS generates a property My.Resources.Something and that is how you access the resource in code. The type of that property depends on the format of the original file. If it was a text file then the property will be type String. If it was a JPEG file then the property will be type Image. For a number of file types, the property will be type Byte(), i.e. Byte array.

    How you use the property value in code depends on the type and what you want to do with it. If you actually need a file then you will have to extract the resource and save it as a file first. In the case of an MP4 to be played in Windows Media Player, the data would be a Byte array and you would need a file in order to give WMP the path. Your code would look something like this:
    vb.net Code:
    1. IO.File.WriteAllBytes(filePath, My.Resources.MyVideo)

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