Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Articles » Create Your Own Custom File Type

Create Your Own Custom File Type

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.

Author Rank:
Total page views :  17150
Total downloads :  674
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
CustomFileType.zip
 
Click Here for 6 Months Free! Powerful ASP.NET Hosting at your Fingertips!
Become a Sponsor

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


Login to add your contents and source code to this article
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
CustomFileType.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Try to modify command line by bonie On September 23, 2009
Nice article...
But  i try to create new file type with only rename XML File to my custom File type (suck us *.SLP) and it succesfully when it deployment...

In other ways, my custom file type was not working when i double click...
It meant my application was open but the resources of My custom file (*.SLP) was not show up...

I wanted to ask you how to create Command Line Argument based of XML file
so i can to modify of command line into my custom file type(*.SLP)

Thank u.... and don't give up...
Reply | Email | Delete | Modify | 
Helpme to Create My own by shariq On December 18, 2009
Hi Scott lysle ,
                     Please help me About that
i want to make a Application , My Application Required A password. I want to save my Password in My Custom file. Like (.phb) Please helpme to create This
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.