This blog is moved to
http://amalhashim.wordpress.com

Friday, March 13, 2009

File Association

1. Easy way is through the Visual Studio Setup & Deployment project.

How to manually set the file association?

Imports System
Imports Microsoft.Win32

1. Determine an arbitrary file type name.

Public Sub SetFileType(ByVal extension As String, ByVal FileType As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.CreateSubKey(extension)
ext.SetValue("", FileType)
End Sub

2. Write this file type name to the Registry, associating it with your extension.

Public Sub SetFileType(ByVal extension As String, ByVal FileType As String)
Dim rk As RegistryKey = Registry.ClassesRoot

Dim ext As RegistryKey = rk.CreateSubKey(extension)
ext.SetValue("", FileType)
End Sub

3. Add a description for your file type.

Public Sub SetFileDescription(ByVal FileType As String, ByVal Description As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.CreateSubKey(FileType)
ext.SetValue("", Description)
End Sub

4. Add actions.

Public Sub AddAction(ByVal FileType As String, ByVal Verb As String, ByVal ActionDescription As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType,True).CreateSubKey("Shell").CreateSubKey(Verb)
ext.SetValue("", ActionDescription)
End Sub

5. Setting Action to do something.

Public Sub SetExtensionCommandLine(ByVal Command As String, ByVal FileType As String, ByVal CommandLine As String, Optional ByVal Name As String = "")
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType).OpenSubKey("Shell").OpenSubKey(Command, True).CreateSubKey("Command")
ext.SetValue(Name, CommandLine)
End Sub

Make sure you set “CommandLine” exactly as it should be as if you typed in the line from the MS-Dos or from the Command Prompt. Instead of using a File name, however, use “%1”.

6. To set a default action

Public Sub SetDefaultAction(ByVal FileType As String, ByVal Verb As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType).OpenSubKey("Shell")
ext.SetValue("", Verb)
End Sub

7. Setting icon

Public Sub SetDefaultIcon(ByVal FileType As String, ByVal Icon As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType)
ext.SetValue("DefaultIcon", Icon)
End Sub

No comments: