Introduction:
This article describes a simple approach to creating a
custom file type. In the example provided, a custom file type is created around
a serializable class that is used as a data container holding all of the
elements necessary to support an application designed to interact with the
defined file type. A separate module is used to serialize and deserialize files
of this user defined file type.
It is possible to include more than a single data
container class as the basis for the file type; placing a collection of objects
into a hash table will allow for greater complexity but will still operate in
the same manner. The approach is useful if you are in need of a way to create a
file type that is specific to a particular application.
Getting Started:
In order to get started, unzip the downloaded files and
open the project provided. Within the project you will find three main files:
frmMain.vb, ProjectSerializer.vb, and PersonalData.vb.
frmMain.vb: This is the
application and it is used to represent an application that interacts
exclusively with the custom file type. This form contains a collection of text
boxes used to capture or display values associated with instances of the
PersonalData class. It only contains three primary functions, one to save a
file as the custom type, one to open a file on startup, and one to open a file
of the custom type through a file open dialog. To open a file, the application
captures the path to the file by means of an OpenFileDialog, the path of the
file along with a new instance of the PersonalData class is subsequently passed
to the ProjectSerializer's deserialize function which in turn deserializes the
content of the stored file and passes it back to the Open File function. Once
the deserialized values are applied back to the PersonalData object, the Open
File function populates the form textboxes from the values contained in that
object. To save a file, the values currently displayed in the form's text boxes
are gathered and used to populate the properties contained in a new instance of
the PersonalData class. The class along with the file path captured from a
SaveFileDialog are passed to the ProjectSerializer's serialize method where a
binary formatter is used to serialize the object's content into a file created
at the specified file path.
ProjectSerializer.vb: This
is a module that contains two functions; one to serialize the data contained on
the form and one to deserialize the data from a stored file saved as the custom
file type. Both methods use the binary formatter although it would work equally
well with soap formatter.
PersonalData.vb: A
serializable class used contain the data associated with an instance of a custom
file type as generated from the fields on in the frmMain form. One important
point to make here is that this class is defined as serializable; if it were not
the operation would fail.
Each of the files mentioned herein are contained in the
sample application; the code is fairly well annotated and should be straight
forward and easy to follow. Sufficient annotation is contained within these
files so they should be easy enough to understand. At this point, you may wish
to open the project, run it to see how it works, and then examine the code files
to see how the project was constructed.
Creating a File Type Association.
The process of creating a file type association through
the use of the Visual Studio 2005 setup and deployment project is far easier to
manage than it once was; you can manually code the necessary information into
the application, and depending upon the installation package you are using, you
may need to do that. If you are interested in that approach, take a look at
this link,
http://www.vbcity.com/forums/faq.asp?fid=15&cat=Registry#TID72502,
[see File Associations the hard way].
First off, in the main form of the application, you
will need to modify the form load event handler to respond to the receipt of a
command line argument (the file name), this is now a trivial bit of code and it
should look something like this:
Private
Sub frmMain_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs)
Handles MyBase.Load
'check each parameter to get the file name (there is
only one though)
For Each param
As String
In My.Application.CommandLineArgs
Try
' pass the file path if it exists
OpenFromPath(param)
Catch
'do nothing, just open the application with no file
End Try
Next param
End
Sub
In this example, when the form loads, it will examine
the contents of My.Application.CommandLineArgs to see if it contains a file
name; since that is all we are going pass in, it will either be empty or will
contain a file path. If a file path is present, it will passed to a subroutine
called, "OpenFromPath" which captures the data from the file and populates the
form's text boxes.
In order to pass the file path to the command line
arguments, you need to set up a couple of things in the Setup and Deployment
project. To begin, add a setup and deployment project to the existing solution
and configure it to suit your requirements (the sample code include a setup
project). Once the project has been added, click on the setup project's name in
the solution explorer, click on the "View" and then click on the "File Types"
option. This will bring up a File Types Designer in the main window of Visual
Studio.
Once the file type designer has been displayed, right
click on "File Types on Target Machine" and the click on the "Add File Type"
option. This will add an empty file type to the tree view, select the new file
type's node and look in the property editor:
Figure 1: File Type Property Editor
In the property editor, set the name to match the name
of the custom type, set the command to point to the application (as the Primary
output from the application), key in a description, set the extension to the
custom file type's extension, and set an icon for the file type. Having set
those values, click on the Open node.
Figure 2: The Open node under the custom file type
Once you have clicked on the open node, the property
editor will show these properties:
Figure 3: Property Editor Set to the Open Node
These default properties are correct in that the
default process is set to "open" and the "%1" is set to pass the file name of a
selected file to the application's command line arguments on startup. After a
user installs the application, when they double click on a file of the custom
file type, it will pass the file path to the application and open it. Also, if
the user right clicks on the custom file type icon, they will be able to select
the "open with" option and be presented with a link to your application as
indicated in the following figure:
Figure 4: Open With option in Windows Explorer context menu