Click here to Skip to main content
Click here to Skip to main content

File Type Creator

, 14 Sep 2006
Rate this:
Please Sign up or sign in to vote.
Shows you how to create your own file types and use them with your program.
Sample image

Introduction

Ever since I started programming I wanted to know how to create my own file types to go along with my programs. It's actualy quite simple because VS.NET has it's own built in file types editor that comes along with your deployment projects. In this article I will show you how to create your own file types using the built in editor, how to open them with your program, and use them to open your program.

I hope you will stay and read the end of the article for a good laugh or two Smile | :)

Using The Source Code

Along with the downloadable source code files is a Deployment folder that contains the deployment project you'll need to add to the program. For the file type program to work you'll need to install it on your computer because the file type needs to be registered with the Windows Registry before it will work correctly.

Program Controls

The main project controls in this application are the Open Button - btnOpen, Save Button - btnSave, and 3 text boxes; TextBox1, TextBox2, and txtMessage.

The CommandLine()

Every time you run a program with Windows the variable CommandLine() holds the file path of the program you just opened as a String.  However, if you were to double click on a file that opens a program, i.e. .bmp would open Windows Paint, then the CommandLine() would hold the file path Strings of the program and the file that was clicked seperated by a space.  It made for some interesting turnouts as you'll see at the end of the article.

Source Code

When your program first starts up you need to run a test to see if the program was started by the user opening one of the registered files (not the program itself).  You can do this by testing the CommandLine() for the String ".mnft" (or whatever file type you registered).  If the CommandLine() does in fact contain that String the program makes the necessary calculations and then skips right to the Function functionFileOpen()

(.mnft = My New File Type)

Form_Load

</FONT><FONT color=#008000 size=2><FONT size=2><P></FONT><FONT color=#008000 size=2>
  '//Searches the CommandLine() for the String ".mnft"</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> InStr(System.Environment.CommandLine, ".mnft") </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>     '//Declare variables</FONT></P><P><FONT color=#0000ff size=2> </FONT><FONT color=#0000ff size=2>    <FONT color=#008000>'//holds the CommandLine()</FONT></FONT></P><P><FONT color=#0000ff size=2>   Dim</FONT><FONT size=2> cmdLine </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = System.Environment.CommandLine</P><P></FONT><FONT color=#0000ff size=2>     <FONT color=#008000>'//holds the value of the character just read from the CommandLine()</FONT></FONT></P><P><FONT color=#0000ff size=2>   Dim</FONT><FONT size=2> charInLine </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>     <FONT color=#008000>'//holds the number of quotation marks chars read from the CommandLine()</FONT></FONT></P><P><FONT color=#0000ff size=2>   Dim</FONT><FONT size=2> quoteCharStop </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = 0</P><P></FONT><FONT color=#0000ff size=2>     <FONT color=#008000>'//holds the place in the CommandLine() and moves backwards</FONT></FONT></P><P><FONT color=#0000ff size=2>   Dim</FONT><FONT size=2> placeInString </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = cmdLine.Length - 1</P><P></FONT><FONT color=#008000 size=2>     '//while we do not have two quotation mark chars and</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>     '//we are not at the beggining of the CommandLine() String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>   While</FONT><FONT size=2> quoteCharStop < 2 </FONT><FONT color=#0000ff size=2>And</FONT><FONT size=2> placeInString > 0</P><P>      charInLine = cmdLine.Substring(placeInString, 1)</P><P></FONT><FONT color=#008000 size=2>        '//if charInLine = quotation mark, add one to qouteCharStop</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>        '//don't copy quotation mark char to newLine</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>      If</FONT><FONT size=2> charInLine = Chr(34) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>         quoteCharStop = quoteCharStop + 1</P><FONT color=#008000 size=2><P><FONT color=#008000 size=2>       '//newLine is declared as Public() right below the "Windows Generated Code"</FONT></P><P><FONT color=#008000 size=2>       '//Important! once complete, newLine will be the path of the file the user clicked</FONT></P></FONT><P></FONT><FONT color=#0000ff size=2>      Else</FONT><FONT size=2> : newLine = charInLine & newLine</P><P></FONT><FONT color=#0000ff size=2>      End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>        '//remeber, we're working backwards</P></FONT><FONT size=2><P>      placeInString = placeInString - 1</P><P></FONT><FONT color=#0000ff size=2>   End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>While</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>  '//goto function to open the file that</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>  '//the user just clicked</P></FONT><FONT size=2><P>functionFileOpen()</P><P></FONT><FONT color=#008000 size=2>  '//set file paths to textboxes</P></FONT><FONT size=2><P>TextBox1.Text = cmdLine</P><P>TextBox2.Text = newLine</P></FONT></FONT>

functionFileOpen()

<FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Function</FONT><FONT size=2> functionFileOpen()</P><P></FONT><FONT color=#008000 size=2>     '//Declare variables</FONT><FONT color=#008000 size=2></P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>     <FONT color=#008000>'//allText holds all the text that is read from the file</FONT></FONT></P><P><FONT color=#0000ff size=2>     <FONT color=#008000>'//lineOfText holds each line of text as it is read from the file</FONT></FONT></P><P><FONT color=#0000ff size=2>   Dim</FONT><FONT size=2> allText, lineOfText </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>   Try</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>        '//open file (file Number, FilePath of the file the _</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>        '//user opened, Open to get Input from the file)</P></FONT><FONT size=2><P>      FileOpen(1, newLine, OpenMode.Input)</P><P></FONT><FONT color=#008000 size=2>        '//do until End Of File</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>      Do</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Until</FONT><FONT size=2> EOF(1)</P><P></FONT><FONT color=#008000 size=2>           '//read one line at a time from file</P></FONT><FONT size=2><P>         lineOfText = LineInput(1)</P><P></FONT><FONT color=#008000 size=2>           '//add each line read and Return Char to allText</P></FONT><FONT size=2><P>         allText = allText & lineOfText & vbCrLf</P><P></FONT><FONT color=#0000ff size=2>      Loop</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>        '//puts all the text from the file into txtMessage</P></FONT><FONT size=2><P>      txtMessage.Text = allText</P><P></FONT><FONT color=#008000 size=2>        '//lol, take this line out and see what happens!</P></FONT><FONT size=2><P>      txtMessage.Select(1, 0)</P><P></FONT><FONT color=#0000ff size=2>   Catch</FONT><FONT size=2> ex </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Exception</P><P>      MsgBox("Sorry, Can't Open File.")</P><P></FONT><FONT color=#0000ff size=2>   Finally</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>        '//closes file</P></FONT><FONT size=2><P>      FileClose(1)</P><P></FONT><FONT color=#0000ff size=2>   End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Try</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Function</FONT>
</P>

Using The File Types Editor

Opening The File Types Editor

Open the project you would like to add a file type to in VS.NET. On the main menu in VS.NET click File, scroll to Add Project (with the arrow), and click on New Project. Create your deployment project by selecting it in the dialog box and name it whatever you want to. Now, in Solution Explorer (probably on the right side of your screen) click ONCE on your newly created deployment project. In the main menu click View, go down one menu item to Editor, now click on File Types (Editor-->File Types).  If the deployment project is not selected there will be no Editor menu option.

Creating Your Own File Types

Now that you know how to open the File Types Editor it's time to learn how to create your own file types.

In the File Types Editor right click on File Types on Target Machine and click Add File Type. Select the new file type and set it's Properties to...

Name - My New File Type

Command - Primary Output for...your program    Note: You'll need to set up the File System on Target Machine prior to this

Extensions - mnft     Note: No period!

Icon - Pick one!

There! That's all there is to it!  Have fun exploring all the possibilities, like special encryption files that only your program can Code/De-Code.

Whoops!

OK, time to examine the fun part of the Form_Load event procedure.

<FONT size=2><P></FONT><FONT color=#0000ff size=2>  While</FONT><FONT size=2> quoteCharStop < 2 </FONT><FONT color=#0000ff size=2>And</FONT><FONT size=2> placeInString > 0</P><P>     charInLine = cmdLine.Substring(placeInString, 1)</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>  If</FONT><FONT size=2> charInLine = Chr(34) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>     quoteCharStop = quoteCharStop + 1</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>  Else</FONT><FONT size=2> : newLine = charInLine & newLine</P><P></FONT><FONT color=#0000ff size=2>     End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If<FONT size=2></P><P>  placeInString = placeInString - 1</P><P></FONT><FONT color=#0000ff size=2>  End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>While</FONT></FONT>
</P>

When I first coded this I made three mistakes.

1.  Instead of using Chr(34) I used Chr(32)       Don't laugh, it's not funny Smile | :)

2.  I used the typical this = this + somethingElse. But notice how we are building the string backwards!  So I needed to use the command this = somethingElse + this instead.

3.  I left the quotation marks in the file path String from the CommandLine().

So, with those three mistakes combined, imagine my surprise when the text in TextBox2 read

"tfnm.olleh/potkseD/cisaBlausiV/sgnitteS dnA

Whoops!  It only looped until I had two Spaces, Copied it backwards, and included the quotation mark!  I almost rolled off my chair upon seeing that one!

The reason you cannot leave the quotation marks in is because the variable already saves the file path as a string, so if you keep the quotation marks in there it won't open the file because file paths just don't include quotation marks.  Smile | :)

The End

I hope this article helps everyone out some.  Thanks for reading and feel free to ask questions!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yep-ItsMe
Web Developer
United States United States
3+ years Visual Basic.NET programming experience. A little ASP.NET and HTML as well.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMuhammady15-Mar-12 3:22 
GeneralGetCommandLineArgs PinmemberMalte Klena14-Sep-06 22:34 
GeneralRe: GetCommandLineArgs [modified] PinmemberYep-ItsMe15-Sep-06 1:32 
GeneralIcons and other [modified] PinmemberYep-ItsMe15-Aug-06 11:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Sep 2006
Article Copyright 2006 by Yep-ItsMe
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid