|
VB .NET Techniques and References
|
|
|
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.
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,
- create a control that will contain the image
(for example a PictureBox, but this will work for any control that has an Image or BackgroundImage property)
- In the Image or BackgroundImage property, click on the three dots (...)
- Select Local resource and press the Import button.
- Browse to the file you want and press all the necessary confirmation buttons
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
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:
- Create a PictureBox control
- Click the three dots (...) in its Image property
- Select Global resource and press the Import button.
- Browse to the file you want and press all the necessary confirmation buttons
- In the Solution Explorer window, you will see that your file has now been added as a resource in the Resources folder for the
project.
You will not see it in the Resources Explorer, but it is now a project resource that you access either statically or programmatically (See part 4).
- Repeat this process with the same Picturebox to upload as many pictures as you want. You will need to clear the previous image in the Image property before
- Once you have added all the resources you need, delete the PictureBox control and Build your solution. This will not delete your image reources.
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:
- 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.
- Put all the media files needed by your program in that folder.
- 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"
- 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:
- The
Split method of the String class
identifies the substrings of a string that are delimited by one or more characters
specified in an array, then places the substrings into a String array.
-
Integer.Parse(string)
returns the integer represented by string
(The link is for Int16.Parse. I could not find a link for Integer.Parse but the method is the
same)
This page is maintained by
Sophie Quigley
(cps613@cs.torontomu.ca)
Last modified
Thursday, 03-Oct-2024 18:53:02 EDT