Click here to Skip to main content
15,896,201 members
Articles / Desktop Programming / Windows Forms

File Association in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.06/5 (39 votes)
11 Aug 2010CDDL4 min read 356.3K   6.4K   132  
Easily associate your programs with file types (.jpg, .html, .mp3) with just 2 lines of Visual Basic code
Module Main
    Public Sub Main()
        'Running this program on its own will associate it with the .hello extension.
        'Opening a .hello file after this happens will display a dialog with the contents of the 
        'file shown.
        If (My.Application.CommandLineArgs.Count > 0) Then
            Dim adjective As String = My.Computer.FileSystem.ReadAllText(My.Application.CommandLineArgs(0))
            MsgBox("Hello, " & adjective & " world!")
        Else
            My.Computer.Registry.ClassesRoot.CreateSubKey(".hello") _
                .SetValue("", "Hello", Microsoft.Win32.RegistryValueKind.String)
            My.Computer.Registry.ClassesRoot.CreateSubKey("Hello\shell\open\command") _
                .SetValue("", Application.ExecutablePath & " ""%l"" ", Microsoft.Win32.RegistryValueKind.String)
            MsgBox("This program has been associated with the .hello extension. You may now open a .hello file from Windows Explorer.")
        End If
    End Sub
End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions