VB .net Associate file with Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dab
    New Member
    • Aug 2006
    • 3

    #1

    VB .net Associate file with Program

    I want to make a file associate with my program I just created. I'd like text files to open up my app instead but I don't know where to begin. I found a C# example, but am having a hard time converting.

    The File With The Example
    Above is the C# file. I would like it in VB(.net) if anyone wouldn't mind. Or if some one can help me find a tutorial online. (I'm still searching).

    Thanks Everyone. I appreciate it. :)
  • krodman
    New Member
    • Aug 2006
    • 12

    #2
    I've used this in the past, I can't remember where I got it from, but it works great.

    Code:
    Public Class Example
    
        Public Sub RegisterType()
            Dim fileReg As New FileTypeRegistrar
    
            With fileReg
                .FullPath = Path_To_Executable
                .FileExtension = Extension_To_Register
                .ContentType = "application/" & Your_Description
                .IconIndex = Icon_Index_In_Application
                .IconPath = Path_To_Executable
                .ProperName = Name_Of_Executable
                .CreateType()
            End With
    
        End Sub
    
    End Class
    
    Public Class FileTypeRegistrar
    
    #Region "Properties & Property Variables"
        Private _ProperName As String
        Public Property ProperName() As String
            Get
                Return _ProperName
            End Get
            Set(ByVal Value As String)
                _ProperName = Value
            End Set
        End Property
    
        Private _ContentType As String
        Public Property ContentType() As String
            Get
                Return _ContentType
            End Get
            Set(ByVal Value As String)
                _ContentType = Value
            End Set
        End Property
    
        Private _FullPath As String
        Public Property FullPath() As String
            Get
                Return _FullPath
            End Get
            Set(ByVal Value As String)
                _FullPath = Value
            End Set
        End Property
    
        Private _FileExtension As String
        Public Property FileExtension() As String
            Get
                Return _FileExtension
            End Get
            Set(ByVal Value As String)
                _FileExtension = Value.Replace(".", "")
            End Set
        End Property
    
        Private _IconPath As String
        Public Property IconPath() As String
            Get
                Return _IconPath
            End Get
            Set(ByVal Value As String)
                _IconPath = Value
            End Set
        End Property
    
        Private _IconIndex As Integer
        Public Property IconIndex() As Integer
            Get
                Return _IconIndex
            End Get
            Set(ByVal Value As Integer)
                _IconIndex = Value
            End Set
        End Property
    #End Region
    
    #Region "Public Methods"
        Public Sub CreateType()
            Dim fileName As String = Path.GetFileNameWithoutExtension(FullPath)
            Dim Ext As String = "." & FileExtension.ToLower
            Dim extKey As RegistryKey = Registry.ClassesRoot.CreateSubKey(Ext)
    
            extKey.SetValue("", fileName)
            extKey.SetValue("Content Type", ContentType)
            extKey.Close()
    
            Dim mainKey As RegistryKey = Registry.ClassesRoot.CreateSubKey(fileName)
            Dim defIconKey As RegistryKey = mainKey.CreateSubKey("DefaultIcon")
    
            defIconKey.SetValue("", IconPath & ", " & IconIndex)
            defIconKey.Close()
    
            Dim shellKey As RegistryKey = mainKey.CreateSubKey("shell")
            Dim OpenKey As RegistryKey = shellKey.CreateSubKey("Open")
            Dim cmdKey As RegistryKey = OpenKey.CreateSubKey("command")
    
            cmdKey.SetValue("", """" & FullPath & " %1""")
            cmdKey.Close()
            OpenKey.Close()
            shellKey.Close()
            mainKey.Close()
    
        End Sub
    
        Public Sub DeleteType()
            Dim fileName As String = Path.GetFileNameWithoutExtension(FullPath)
            Dim Ext As String = "." & FileExtension.ToLower
    
            Registry.ClassesRoot.DeleteSubKey(Ext)
            Registry.ClassesRoot.DeleteSubKey(fileName)
    
        End Sub
    #End Region
    
    End Class

    Comment

    Working...