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.
The main project controls in this application are the Open Button -
btnOpen, Save Button -
btnSave, and 3 text boxes;
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.
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
(.mnft = My New File Type)
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.
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>
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
So, with those three mistakes combined, imagine my surprise when the text in TextBox2 read
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. :)
I hope this article helps everyone out some. Thanks for reading and feel free to ask questions!