Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 32,015 times

Contents

Related Categories

File Assocation - File Associaton

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.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [36] File Assocation

    Posted by DEVILSAN on 19 Sep 2006

    Hi Can you please also tell how to do it in .Net using VB.Net or C# ...

  • Reversing this action

    Posted by Matt2977 on 16 Aug 2005

    This looks like it could be the answer to my problem, but in case i stuff it up somehow could you tell me how to reverse the changes that are made to the registry by this code please?
    Thanks

  • open file from vb

    Posted by onggan on 09 Aug 2005

    please help me..,
    i have problem, i can't load file word (*.doc) from vb, can u tell me how to do it??
    thx b4.
    please send ur coding to niel_onggan@yahoo.com

  • Posted by James Crowley on 29 Apr 2003

    That's your procedure that does whatever when its been given a filename (ie loading the file into a textbox or something)

  • OpenFile

    Posted by weirdo on 24 Apr 2003

    The code is working fine, except for the OpenFile bit.
    The comment says to call your openfile procedure.
    What is the OpenFile command?