tags:

views:

413

answers:

2

Hi,

I need to change the icon of a specific extention using vb.net programming .. How to do .. ?

A: 

In Windows, file extensions and their associated icons and programs are stored in the Registry: HKEY_CLASSES_ROOT\ for system-wide associations.

Starting with Windows XP, there's also HKEY_CURRENT_USER\Software\Classes\ for the current user's file associations, but so far it's rarely used.

For example, if you want to change the information for .txt, you would first check the default value HKEY_CLASSES_ROOT\.txt\ (in my system it's txtfile), and then again go to the matching key in HKEY_CLASSES_ROOT — in this example, it would be HKEY_CLASSES_ROOT\txtfile\DefaultIcon.

But I don't use VB.NET, so I cannot help more. (And there probably exists a better way to do it than described here.)

grawity
A: 

Ok, threw this together in VB 2005, but it should work in VB 2008 too.

Imports System
Imports Microsoft.Win32.Registry

Public Class Form1
    ' Controls:
    ' txtFT: Textbox, where the user inputs the filetype (eg. ".jpg")
    ' txtIcon: Textbox, where the user inputs the path to the icon (eg. "C:\icon.ico")
    ' btnChangeIcon: Button, to call the function.
    '-----------------------------------------------------------------------------------------------


    Public Sub SetDefaultIcon(ByVal FileType As String, ByVal Icon As String)
        Dim rk As Microsoft.Win32.RegistryKey = ClassesRoot
        Dim rk1 As Microsoft.Win32.RegistryKey = ClassesRoot
        Dim ext As Microsoft.Win32.RegistryKey = rk.OpenSubKey(FileType)
        Dim regtype As String = ext.GetValue("")
        ext = rk1.OpenSubKey(regtype, True).OpenSubKey("DefaultIcon", True)
        ext.SetValue("", Icon)
        MessageBox.Show(ext.ToString)
    End Sub

    Private Sub btnChangeIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeIcon.Click
        SetDefaultIcon(txtFT.Text, txtIcon.Text)
    End Sub
End Class

Tested on Windows XP.

As you can see, it gets the filetype, and gets the (Default) value. This value points to its association, which contains the DefaultIcon key. The user inputs the filetype in "txtFT", and the icon file in "txtIcon". The form is Form1. When the user clicks btnChangeIcon, the SetDefaultIcon function is called. If the user clicks btnChangeIcon with no info entered it can be problematic, so you should add some error handling if you're going down that route. If you're setting it through code you'll be fine though.

For icons that don't have associations I don't know how to do it, other than making associations for them yourself.

  • SP
SP534