It is the little things that make a difference. Adding a bit more effort or detail to one's finished product can leave a total lasting impression. Setting the capability for your program to be the default application for its files is a small thing that can not only improve your applications, and your user's experience but also polish off your application nicely.
Today, I want to show you how to make your application a default application, as well as enabling your application to open its files from inside My Computer.
A default program is the program that Windows uses when you open a particular type of file, such as a music file, an image, or a Web page. Examples of these include opening .docx files in Microsoft Word and .pdf files in Acrobat Reader.
To set a default application, you need to have the proper file associations. As mentioned earlier: Microsoft Word opens .docx files, but it can open a lot of different files, too. You may have many Wwb browsers on your computer; you need to set the default browser to the correct file extension. You may have a lot of graphic design programs on your computer; you need to set each program by the appropriate file extension.
This is usually done automatically by the programs. But how?
The simple answer is the Windows Registry.
The Registry is a hierarchical database that stores low-level settings for Windows and applications that opt to use the Registry. You can store small bits of information, such as recently used files, form sizes and location, and user preferences—to name just a few—in the Windows Registry.
The Windows Registry contains two elements namely keys and values. Registry keys are container objects. Registry values are non-container objects. Keys may contain values and sub keys. There are five predefined root keys:
- HKEY_LOCAL_MACHINE (HKLM)
- HKEY_CURRENT_CONFIG (HKCC)
- HKEY_CLASSES_ROOT (HKCR)
- HKEY_CURRENT_USER (HKCU)
- HKEY_USERS (HKU)
You will save the appropriate settings inside the Windows Registry to make your application a default application.
Create a Windows Forms Visual Basic application and name it anything you like. Include the following on your form:
- Multiline Textbox
- Menu containing the following Menu items:
- Set As Default Application
To be able to launch a document from My Computer, I have decided to create a little demo project so that you can see the whole process, plus the default setting in action.
Add the following Namespace because you will be working with files and you will need the correct .NET classes to be able to open and write files:
Add the following code to save a file to disk:
Private Sub SaveToolStripMenuItem_Click(sender As Object, _ e As EventArgs) Handles SaveToolStripMenuItem.Click SaveFileDialog1.Filter = "HTG Files (*.htg*)|*.htg" If SaveFileDialog1.ShowDialog = _ Windows.Forms.DialogResult.OK Then My.Computer.FileSystem.WriteAllText _ (SaveFileDialog1.FileName, TextBox1.Text, True) End If End Sub
Inside the SaveToolStripMenuItem_Click event, I have made use of the SaveFileDialog object to save a file with a designated name. Pay close attention to the file extension. I have opted to use '.htg' as an extension because I do not have any programs on my system that utilize that extension. The '.htg' extension will come into play again a bit later.
If you test your application now, you will be able to save a file. Make sure you have entered text inside the Textbox. as shown in Figure 1.
Figure 1: Test Text
Figure 2: Saving a File
Add the following code to Open a file and display its contents inside the Textbox:
Private Sub OpenToolStripMenuItem_Click(sender As Object, _ e As EventArgs) Handles OpenToolStripMenuItem.Click OpenFileDialog1.Filter = "*.htg|*.h If OpenFileDialog1.ShowDialog = DialogResult.OK Then OpenFile(OpenFileDialog1.FileName) End If End Sub Private Sub OpenFile(strFileName As String) Dim srStream As New StreamReader(strFileName) TextBox1.Text = srStream.ReadToEnd Me.Text = strFileName srStream.Close() End Sub
When the Open Menu item is clicked, the OpenFileDialog will be displayed, allowing you to browse and select a file to open. The Open logic is shown in the OpenFile Sub that makes use of a StreamReader object to read the file. Lastly, the File's name gets displayed on the Form's TitleBar, as shown in Figure 3.
Figure 3: Opening an .htg file
Add the following code behind the 'Set As Default Application' menu item:
Private Sub SetDefaultToolStripMenuItem_Click _ (sender As Object, e As EventArgs) Handles _ SetDefaultToolStripMenuItem.Click My.Computer.Registry.ClassesRoot.CreateSubKey(".HTG") _ .SetValue("", "HTG", _ Microsoft.Win32.RegistryValueKind.String) My.Computer.Registry.ClassesRoot.CreateSubKey _ ("HTG\shell\open\command").SetValue("", _ Application.ExecutablePath & " ""%l"" ", _ Microsoft.Win32.RegistryValueKind.String) End Sub
Here, I have made use of the built-in Registry classes to save the appropriate entries into the Registry. This sets the application as a default to open any file with the '.htg' extension. Figures 4 and 5 show the entries in the Windows Registry.
Figure 4: HKEY_CLASSES_ROOT\HTG
Figure 5: HKEY_CLASSES_ROOT\HTG\shell\open\command
Navigate to your Debug or Release folder and execute the application from there with Administrator privileges. This will just ensure that the correct application gets stored inside the Registry. Select the 'Set As Default' menu and voilà, your application is now registered to be the default application for any '.htg' files.
Now you have to remember that you can launch any file from anywhere on your computer. At this stage, our application does not know what to do at startup. At this stage, even if you double-click your '.htg' file, it will not display its contents.
You need to check the CommandLineArgs of your program and determine if there is a parameter that was sent to your program. The parameter in this case will be the name of the file. To compensate for this and to allow your application to open the double-clicked file, add the following code:
Private Sub Form1_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load Try If My.Application.CommandLineArgs.Count > 0 Then OpenFile(My.Application.CommandLineArgs(0)) End If Catch ex As Exception End Try End Sub
Please download the attached DefaultApp_Ex zip file (see below the article). It contains what you need to complete this project.
It is all in the small details. Setting your application up as the Default application is not something major the user will notice, but it is indeed a major little detail that the end-user will expect.