Choosing a File Type
In the example in the previous section, I simply named the file c:\temp\userdata.dat. In a real program, you would let the user choose the directory and filename, but you’ll probably want to use your own file extension.
By relying on the serialization feature, you have effectively created your own file type. Although you didn’t actually define the format for the file (the .NET runtime did that), you’re determining what goes into the file. To make the file specific to your program, you can choose a file extension and then associate the file with your program.
For my text display program, I chose the file type .jtx. You can use anything you want. If you use SaveFileDialog and OpenFileDialog controls, you could set the DefaultExt property of each to your custom filename extension. (In my case that would be jtx. Don’t include the dot as in .jtx; you just put in the letters.)
You can also include a filter so that only files of that type show up in the dialog box. This technique adds a professional touch. Use the Filter property for SaveFileDialog and OpenFileDialog. The filter consists of a set of strings separated by the vertical bar (|) or pipe symbol. The one I used looks like this:
JTexter Files (*.jtx)|*.jtx|All Files(*.*)|*.*
There must be an even number of strings. The strings are grouped into pairs, with the first being the text that appears in the File Type drop-down list, and the second being the filename pattern. You can have as many filters as you want, each getting two strings, the text and the pattern. Thus, I’m supplying two filters, as shown in Figure 1.
Figure 1 This Open dialog box has two filters, *.jtx and *.*.
Finally, another professional touch is to associate the file with the program. How you do that depends on the installation software you’re using. The documentation can probably help you. To do it manually, follow these steps:
- Open an Explorer window (for example, by double-clicking My Computer on the desktop).
- Choose Tools > Folder Options.
- In the Folder Options window, click the File Types tab. After waiting—possibly a very long time—for the list to populate, click New.
- Type your extension in the pop-up window as shown in Figure 2. (Don’t click Advanced. You’ll choose the program next.) Click OK.
- Your new extension will appear at the top of the File Types list. Click it, and then click Change.
- You’ll see a message that Windows can’t open the file. Click the
button called "Select the program from a list" and click OK. The Open
With dialog box appears, as shown in Figure 3.
Figure 2 Type jtx for a new filename extension.
Figure 3 Choose your program!
- If your program is already registered with the operating system, you’ll find it in this list. But more likely, you’ll need to click Browse, and then browse to your program’s directory and choose your program. Once there, click Open. Your program will be selected at the top of the list. Click OK, close the Folder Options dialog box, and you’re all set.
Now, when you double-click a file with your extension in it, your program will open. However, unless you added code in your program to open the file upon startup, it won’t open. The procedure I showed earlier for responding to a menu or button click isn’t enough. Here’s where you’ll use the separate LoadFromFile subroutine that I created earlier.
When you register a file type using the method I showed here, and then you double-click a file of your type, Windows will run your program, passing the filename as an argument. Then you can use your form’s Load handler to load the file:
Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim args() As String = Environment.GetCommandLineArgs() If args.Length > 1 Then LoadFromFile(args(1)) End If End Sub
Here, I use Environment.GetCommandLineArgs to get the argument passed in. Then I just call the LoadFromFile subroutine I created earlier, passing in the argument. (That’s why I broke out the LoadFromFile code into its own subroutine.)
Now, when you double-click your file, your program will open and load the file.