|
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, music, videos, etc.
In some applications you will want to imbed the contents of some external files directly
into the compiled VB program.
This will make your program run faster as no I/O will be required at run time,
but will also increase the size of your dll file.
In .NET this is accomplished by turning each file into an
application resource.
Here are the steps to turn an external file into a project resource:
- Open the Resource Editor for the project:
To do this, double-click on "My Project" in the Solution Explorer,
and select Resources > General and then click on the link to "create or open assembly sources".
Alternately, you could select "View all files in the Solution Explorer and this will show you all the files in the
"My Project" folder including Resources.resx, which keeps the resources for the project.
Double-clicking on this file also brings up the Resource Editor.
- Select the type of resource you are importing:
Use the leftmost pulldown menu of the Resource Editor for this.
This step will make sure that the next step finds the right type of file.
Generally speaking this pulldown is used to show all the existing resources of a specific type.
- Add and name the file as a project resource:
Using the second pulldown menu of the Resource Editor,
select Add Resource > Add Existing File... and add your file. The other options also work.
Once the file is added as a resource, you will see the name of that resource along with an icon indicating its type.
Once you have added all the resources you need, Build the project.
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.
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
Wednesday, 13-Sep-2023 00:54:42 EDT