File Type Creator






2.72/5 (16 votes)
Aug 12, 2006
4 min read

43799

876
Shows you how to create your own file types and use them with your program.

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 :)
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
'//holds the CommandLine()
Dim cmdLine As String = System.Environment.CommandLine
'//holds the value of the character just read from the CommandLine()Dim charInLine As String
'//holds the number of quotation marks chars read from the CommandLine()Dim quoteCharStop As Integer = 0
'//holds the place in the CommandLine() and moves backwards
Dim placeInString As Integer = cmdLine.Length - 1
'//while we do not have two quotation mark chars and
'//we are not at the beggining of the CommandLine() String While quoteCharStop < 2 And placeInString > 0charInLine = cmdLine.Substring(placeInString, 1)
'//if charInLine = quotation mark, add one to qouteCharStop '//don't copy quotation mark char to newLine If charInLine = Chr(34) ThenquoteCharStop = quoteCharStop + 1
'//newLine is declared as Public() right below the "Windows Generated Code"
'//Important! once complete, newLine will be the path of the file the user clicked
Else : newLine = charInLine & newLine End If '//remeber, we're working backwardsplaceInString = placeInString - 1
End While '//goto function to open the file that '//the user just clickedfunctionFileOpen()
'//set file paths to textboxesTextBox1.Text = cmdLine
TextBox2.Text = newLine
functionFileOpen()
Private Function functionFileOpen() '//Declare variables '//allText holds all the text that is read from the file'//lineOfText holds each line of text as it is read from the file
Dim allText, lineOfText As String
Try '//open file (file Number, FilePath of the file the _ '//user opened, Open to get Input from the file)FileOpen(1, newLine, OpenMode.Input)
'//do until End Of File Do Until EOF(1) '//read one line at a time from filelineOfText = LineInput(1)
'//add each line read and Return Char to allTextallText = allText & lineOfText & vbCrLf
Loop '//puts all the text from the file into txtMessagetxtMessage.Text = allText
'//lol, take this line out and see what happens!txtMessage.Select(1, 0)
Catch ex As ExceptionMsgBox("Sorry, Can't Open File.")
Finally '//closes fileFileClose(1)
End TryEnd Function
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.
While quoteCharStop < 2 And placeInString > 0charInLine = cmdLine.Substring(placeInString, 1)
If charInLine = Chr(34) ThenquoteCharStop = quoteCharStop + 1
Else : newLine = charInLine & newLine End IfplaceInString = placeInString - 1
End While
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 :)
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. :)
The End
I hope this article helps everyone out some. Thanks for reading and feel free to ask questions!