Imports System Imports System.Security Imports System.Collections Imports Microsoft.Win32 'The Org.Mentalis.Utilities namespace implements several useful utilities that are missing from the standard .NET framework. Namespace Org.Mentalis.Utilities '/// List of commands. Friend Structure CommandList '/// '/// Holds the names of the commands. '/// Public Captions As ArrayList '/// '/// Holds the commands. '/// Public Commands As ArrayList End Structure '/// Properties of the file association. Friend Structure FileType '/// '/// Holds the command names and the commands. '/// Public Commands As CommandList '/// '/// Holds the extension of the file type. '/// Public Extension As String '/// '/// Holds the proper name of the file type. '/// Public ProperName As String '/// '/// Holds the full name of the file type. '/// Public FullName As String '/// '/// Holds the name of the content type of the file type. '/// Public ContentType As String '/// '/// Holds the path to the resource with the icon of this file type. '/// Public IconPath As String '/// '/// Holds the icon index in the resource file. '/// Public IconIndex As Short End Structure '/// Creates file associations for your programs. '/// The following example creates a file association for the type XYZ with a non-existent program. '///


VB.NET code
'/// '/// Dim FA as New FileAssociation '/// FA.Extension = "xyz" '/// FA.ContentType = "application/myprogram" '/// FA.FullName = "My XYZ Files!" '/// FA.ProperName = "XYZ File" '/// FA.AddCommand("open", "C:\mydir\myprog.exe %1") '/// FA.Create '/// '///
C# code
'/// '/// FileAssociation FA = new FileAssociation(); '/// FA.Extension = "xyz"; '/// FA.ContentType = "application/myprogram"; '/// FA.FullName = "My XYZ Files!"; '/// FA.ProperName = "XYZ File"; '/// FA.AddCommand("open", "C:\\mydir\\myprog.exe %1"); '/// FA.Create(); '/// '///
Public Class FileAssociation '/// Initializes an instance of the FileAssociation class. Public Sub New() FileInfo = New FileType() FileInfo.Commands.Captions = New ArrayList() FileInfo.Commands.Commands = New ArrayList() End Sub '/// Gets or sets the proper name of the file type. '/// A String representing the proper name of the file type. Public Property ProperName() As String Get Return FileInfo.ProperName End Get Set(ByVal Value As String) FileInfo.ProperName = Value End Set End Property '/// Gets or sets the full name of the file type. '/// A String representing the full name of the file type. Public Property FullName() As String Get Return FileInfo.FullName End Get Set(ByVal Value As String) FileInfo.FullName = Value End Set End Property '/// Gets or sets the content type of the file type. '/// A String representing the content type of the file type. Public Property ContentType() As String Get Return FileInfo.ContentType End Get Set(ByVal Value As String) FileInfo.ContentType = Value End Set End Property '/// Gets or sets the extension of the file type. '/// A String representing the extension of the file type. '/// If the extension doesn't start with a dot ("."), a dot is automatically added. Public Property Extension() As String Get Return FileInfo.Extension End Get Set(ByVal Value As String) If (Value.Substring(0, 1) <> ".") Then Value = "." + Value FileInfo.Extension = Value End Set End Property '/// Gets or sets the index of the icon of the file type. '/// A short representing the index of the icon of the file type. Public Property IconIndex() As Short Get Return FileInfo.IconIndex End Get Set(ByVal Value As Short) FileInfo.IconIndex = Value End Set End Property '/// Gets or sets the path of the resource that contains the icon for the file type. '/// A String representing the path of the resource that contains the icon for the file type. '/// This resource can be an executable or a DLL. Public Property IconPath() As String Get Return FileInfo.IconPath End Get Set(ByVal Value As String) FileInfo.IconPath = Value End Set End Property '/// Adds a new command to the command list. '/// The name of the command. '/// The command to execute. '/// Caption -or- Command is null (VB.NET: Nothing). Public Sub AddCommand(ByVal Caption As String, ByVal Command As String) If (Caption Is Nothing OrElse Command Is Nothing) Then Throw New ArgumentNullException() FileInfo.Commands.Captions.Add(Caption) FileInfo.Commands.Commands.Add(Command) End Sub '/// Creates the file association. '/// Extension -or- ProperName is null (VB.NET: Nothing). '/// Extension -or- ProperName is empty. '/// The user does not have registry write access. Public Sub Create() ' remove the extension to avoid incompatibilities [such as DDE links] Try Remove() Catch e as ArgumentException ' the extension doesn't exist End Try ' create the exception If (Extension = "" OrElse ProperName = "") Then Throw New ArgumentException() Dim cnt As Integer Try Dim RegKey As RegistryKey = Registry.ClassesRoot.CreateSubKey(Extension) RegKey.SetValue("", ProperName) If (Not ContentType Is Nothing AndAlso ContentType <> "") Then RegKey.SetValue("Content Type", ContentType) RegKey.Close() RegKey = Registry.ClassesRoot.CreateSubKey(ProperName) RegKey.SetValue("", FullName) RegKey.Close() If (IconPath <> "") Then RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\" + "DefaultIcon") RegKey.SetValue("", IconPath + "," + IconIndex.ToString()) RegKey.Close() End If For cnt = 0 To FileInfo.Commands.Captions.Count - 1 RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\" + "Shell" + "\" + CType(FileInfo.Commands.Captions(cnt), String)) RegKey = RegKey.CreateSubKey("Command") RegKey.SetValue("", FileInfo.Commands.Commands(cnt)) RegKey.Close() Next cnt Catch Throw New SecurityException() End Try End Sub '/// Removes the file association. '/// Extension -or- ProperName is null (VB.NET: Nothing). '/// Extension -or- ProperName is empty -or- the specified extension doesn't exist. '/// The user does not have registry delete access. Public Sub Remove() If (Extension Is Nothing OrElse ProperName Is Nothing) Then Throw New ArgumentNullException() If (Extension = "" OrElse ProperName = "") Then Throw New ArgumentException() Registry.ClassesRoot.DeleteSubKeyTree(Extension) Registry.ClassesRoot.DeleteSubKeyTree(ProperName) End Sub '/// Holds the properties of the file type. Private FileInfo As FileType End Class End Namespace