File Assocation

File Associaton

To get the tmg (or any other) extension to be recognised in Explorer (or any other program), you need to do two things:
1) Register the tmg extension so that explorer knows to run your application when a file of that type is clicked on
2) Provide a description for the extension (ie Word Document)
3) and finally, once your program has been run, get it to open the file.

So, for #1, you can use the following code. Read the comments to see  what you need to change. Note that you will need to compile your program to an EXE for this to work (so that explorer can call your program)

'// Registry windows api calls
Private Declare Function RegCreateKey& Lib "advapi32.DLL" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpszSubKey As String, lphKey As Long)
Private Declare Function RegSetValue& Lib "advapi32.DLL" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpszSubKey As String, ByVal fdwType As Long, ByVal lpszValue As String, ByVal dwLength As Long)
'// Required constants
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const MAX_PATH = 256&
Private Const REG_SZ = 1

'// procedure you call to associate the tmg extension with your program.
Private Sub MakeDefault()
    Dim sKeyName  As String  '// Holds Key Name in registry.
    Dim sKeyValue As String  '// Holds Key Value in registry.
    Dim ret       As Long    '// Holds error status if any from API calls.
    Dim lphKey    As Long    '// Holds created key handle from RegCreateKey.
   
    '// This creates a Root entry called "TextMagic"
    sKeyName = "TextMagic" '// Application Name
    sKeyValue = "TextMagic Document" '// File Description
    ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)
    ret = RegSetValue&(lphKey&, Empty, REG_SZ, sKeyValue, 0&)

    '// This creates a Root entry called .tmg associated with "TextMagic".
    sKeyName = ".tmg" '// File Extension
    sKeyValue = "TextMagic" '// Application Name
    ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)

    ret = RegSetValue&(lphKey, Empty, REG_SZ, sKeyValue, 0&)

    '//This sets the command line for "TextMagic".
    sKeyName = "TextMagic" '// Application Name
    If Right$(App.Path,1) = "\" Then
        sKeyValue = App.Path & App.EXEName & ".exe %1" '// Application Path
    Else
        sKeyValue = App.Path & "\" & App.EXEName & ".exe %1" '// Application Path
    End If
    ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)
    ret = RegSetValue&(lphKey, "shell\open\command", REG_SZ, sKeyValue, MAX_PATH)
End Sub

Private Sub Form_Load()
    '// ensure we only register once. When debugging etc, remove the SaveSetting line, so your program will
    '// always attempt to register the file extension
    If GetSetting(App.Title, "Settings", "RegisteredFile", 0) =  0 Then
        '// associate tmg extension with this app
        MakeDefault
        SaveSetting App.Title, "Settings", "RegisteredFile", 1
    End If
End Sub

Now, try compiling your program and then run it. This will associate the file extension with your program. Now create a new text document, and name it test.tmg. Go into explorer, and you will see that explorer recognises test.tmg as a TextMagic Document. Click on the file, and your program will run.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic