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,
download :
http://opensource.mam9.com/t245-topic...