CPS613

VB .NET Techniques and References

Toronto Metropolitan University University

External Files


Table of Contents

Static Files: How to load files at compile time

Dynamic File I/O: How to access files at run time


Static Files: How to load files at compile time

Application Resources: images

Microsoft introduced a new Resource Manager in the summer of 2024 and essentially broke this component of Visual Studio.

If you would like to play sound or video clips this will not work. Please read the next section on Incorporating other media in your application.

What is described below is a temporary workaround for images until the problem is fixed in a later release which we will unfortunately not be able to use in the F2024 semester. Here is the idea: In some applications you will want to imbed the contents of some external files directly into the compiled VB program, or put them all together in one standard external location.

In .NET this is accomplished by turning each file into an application resource.

Because the process is not fully functional any more, we will only describe how to organize pictures pictures and sound clips, in version 17.11 of Visual Studio installed in the labs.

  1. How to compile an image directly into the application
    If you simply want to put a picture in a picture box and never change it during the life of the app, the simplest approach is to imbed it in a control at design time. It will get compiled with that control. To do this, This picture will now be imbedded in the control, and compiled with the application. If you want to access it programmatically, you can find it in the property of the control where you uploaded it. For example, PictureBox1.Image
  2. How to store an image as an external resource and use it
    Instead of compiling the picture file with the application you can add it as a project resource.

    This is one of the broken parts and what is described below is a workaround. Normally this is done using the Resource explorer, adding the resource as a bitmap, but they forgot to include bitmaps as one of the options for new resources. You can, instead add it indirectly by using a PictureBox as a substitute for the Resource Explorer. Here is how:

    The resources will now be imbedded in your application and will show up where appropriate. For example, if you have added an image resource that you want to add in a control, this is usually done with the property called "Image" or "BackgroundImage". When you click on the three dots of that property in the Property window, the resource you have created will be available as a project resource.

    The resource can also be accessed programmatically by using the My.Resources Object.

Incorporating other media in your application

Trying to add add media files as resources does not work in this release of Visual Studio. If you try to do this, you will end up crashing not just your running program, but also possibly the Visual Studio designer. Instead you should keep your media files in a folder that you will include along with your project when you release it, and refer to these files programmatically using a relative path name. Here is how:
  1. You compiled program is located in the bin\Debug\net6.0-windows directory. Relative pathnames will start in that directory, and therefore this is where you should create a subfolder that contains all the media files you will be using in your program, call it something like "Media" or "Sound" or whetever is appropriate for what you are storing.
  2. Put all the media files needed by your program in that folder.
  3. You will be able to access each file programmatically using methods that require a pathname, for example My.Computer.Audio.Play. The parameter to these methods will be the relative pathname, for example "Media\hornsound.wav"
  4. To deploy your app, zip up the entire net.6.0-windows directory. This will include your media folder.

Dynamic Files: How to access files at run time

Interactive I/O Controls and Components

VB has an OpenFileDialog component and a SaveFileDialog component that are used to traverse through your directories to find the pathnames of files to open or save. To use one of these components, drag it into the controls area at the bottom of your design page. You will need to use almost all the properties of these controls. In particular, don't forget to set the Filter and DefaultExt to work with image files. The online help examples include a lot of code writing, but you can actually set these properties at design time which is a lot simpler.

To run one of these dialog boxes, use the ShowDialog method (just like for the options dialog box). This method will return the status of the ShowDialog operation. Return values are of type DialogResult which is an enumeration type that can be used in Select Case statements. If the operation was successful, the pathname of the selected file will be in the FileName property of the control.

Manipulating Directories

Here is how to in Visual Basic.

Parsing File Paths

Here is how to parse a file path in Visual Basic.

Copying Files

Here is how to copy a file in Visual Basic:

Writing Files

Once you have the right pathname for your files, you will need to perform file I/O with these pathnames. To do so, you must first create either a StreamReader or a StreamWriter object. For example,

Imports System.IO 	' Don't forget this imports statement
Class Game
	...
	Dim sw As StreamWriter = New StreamWriter(SaveFileDialog1.FileName)
	sw.Write(stuff)
	sw.WriteLine(more stuff)
	sw.Close()

Reading Files

Reading is similar to writing except that you may also parse the lines you read in to convert comma-delimited information into actual values. The following two methods assist you in this:


This page is maintained by Sophie Quigley (cps613@cs.torontomu.ca)
Last modified Thursday, 03-Oct-2024 18:53:02 EDT