AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

Create file Associations

In your applications, you may need to create file associations for your saved data.  File associations allow your program to be run when a file with a certain extension is double-clicked in explorer.  To do this in VB involves a little bit of registry tinkering.  Now, it is not difficult, as I have written this tip which creates everything that is needed in the registry.

Declarations

Public Type mnuCommands
Captions As New Collection
Commands As New Collection
End Type

Public Type filetype
Commands As mnuCommands
Extension As String
ProperName As String
FullName As String
ContentType As String
IconPath As String
IconIndex As Integer
End Type

Public Const REG_SZ = 1
Public Const HKEY_CLASSES_ROOT = &H80000000

Public Declare Function RegCloseKey Lib _
"advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKey Lib _
"advapi32" Alias "RegCreateKeyA" (ByVal _
hKey As Long, ByVal lpszSubKey As String, _
phkResult As Long) As Long
Public Declare Function RegSetValueEx Lib _
"advapi32" Alias "RegSetValueExA" (ByVal _
hKey As Long, ByVal lpszValueName As String, _
ByVal dwReserved As Long, ByVal fdwType As _
Long, lpbData As Any, ByVal cbData As Long) As Long

Module Code

Add the following code to a module:

Public Sub CreateExtension(newfiletype As filetype)

Dim IconString As String
Dim Result As Long, Result2 As Long, ResultX As Long
Dim ReturnValue As Long, HKeyX As Long
Dim cmdloop As Integer

IconString = newfiletype.IconPath & "," & _
newfiletype.IconIndex

If Left$(newfiletype.Extension, 1) <> "." Then _
newfiletype.Extension = "." & newfiletype.Extension

RegCreateKey HKEY_CLASSES_ROOT, _
newfiletype.Extension,Result
ReturnValue = RegSetValueEx(Result, "", 0, REG_SZ, _
ByVal newfiletype.ProperName, _
LenB(StrConv(newfiletype.ProperName, vbFromUnicode)))

' Set up content type
If newfiletype.ContentType <> "" Then
ReturnValue = RegSetValueEx(Result, _
"Content Type", 0, REG_SZ, ByVal _
CStr(newfiletype.ContentType), _
LenB(StrConv(newfiletype.ContentType, vbFromUnicode)))
End If

RegCreateKey HKEY_CLASSES_ROOT, _
newfiletype.ProperName, Result

If Not IconString = ",0" Then
RegCreateKey Result, "DefaultIcon", _
Result2 'Create The Key of "ProperName\DefaultIcon"
ReturnValue = RegSetValueEx(Result2, _
"", 0, REG_SZ, ByVal IconString, _
LenB(StrConv(IconString, vbFromUnicode)))
'Set The Default Value for the Key
End If

ReturnValue = RegSetValueEx(Result, _
"", 0, REG_SZ, ByVal newfiletype.FullName, _
LenB(StrConv(newfiletype.FullName, vbFromUnicode)))
RegCreateKey Result, ByVal "Shell", ResultX

' Create neccessary subkeys for each command
For cmdloop = 1 To newfiletype.Commands.Captions.Count
RegCreateKey ResultX, ByVal _
newfiletype.Commands.Captions(cmdloop), Result
RegCreateKey Result, ByVal "Command", Result2
Dim CurrentCommand$
CurrentCommand = newfiletype.Commands.Commands(cmdloop)
ReturnValue = RegSetValueEx(Result2, _
"", 0, REG_SZ, ByVal CurrentCommand$, _
LenB(StrConv(CurrentCommand$, vbFromUnicode)))
RegCloseKey Result
RegCloseKey Result2
Next

RegCloseKey Result2
End Sub

Code

Then use this code to create the association:

Dim myfiletype As filetype

myfiletype.ProperName = "MyFile"
myfiletype.FullName = "My File Type"
myfiletype.ContentType = "SomeMIMEtype"
myfiletype.Extension = ".MYF"
myfiletype.Commands.Captions.Add "Open"
myfiletype.Commands.Commands.Add _
"c:\windows\notepad.exe ""%1"""
myfiletype.Commands.Captions.Add "Print"
myfiletype.Commands.Commands.Add _
"c:\windows\notepad.exe ""%1"" /P"
CreateExtension myfiletype

The filetype type is pretty self-explanatory.  Extension contains the file type extension.  Proper name is the name of the type, which you would refer to the type as (do not use spaces).  FullName is the description of the file type.   ContentType is the description that you would see in your Internet browser, if you were to download a file of this type.  You could think of the extension as the "shortcut key", the proper name as the "name", and the fullname as the "caption".

This Commands part of the filetype contains the "verbs" for the file type. When you right-click on a file of this type, you would see these options. Open is set as the default "verb", so when you double click on the file type, it executes the command associated with "Open". You can add others, such as "Print" as needed.

Be careful with registry editing, as it can have disastrous effects.

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/