繁体   English   中英

以编程方式更改扩展程序图标

[英]Changing extension icon programmatically

如何使用VB.net代码更改扩展程序的图标?

在Windows中,文件扩展名及其关联的图标和程序存储在注册表中: HKEY_CLASSES_ROOT\\用于系统范围的关联。

从Windows XP开始,还为当前用户的文件关联提供了HKEY_CURRENT_USER\\Software\\Classes\\ ,但是到目前为止,它很少使用。

例如,如果要更改.txt的信息,则应首先检查默认值HKEY_CLASSES_ROOT\\.txt\\ (在我的系统中为txtfile ),然后再次转到HKEY_CLASSES_ROOT的匹配键-在此示例中,将是HKEY_CLASSES_ROOT\\txtfile\\DefaultIcon

但是我不使用VB.NET,所以我无能为力。 (而且可能存在比此处所述更好的方法。)

好的,在VB 2005中将其组合在一起,但是在VB 2008中也应该起作用。

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

在Windows XP上测试。

如您所见,它获取文件类型,并获取(默认)值。 此值指向其关联,其中包含DefaultIcon密钥。 用户在“ txtFT”中输入文件类型,在“ txtIcon”中输入图标文件。 形式是Form1。 当用户单击btnChangeIcon时,将调用SetDefaultIcon函数。 如果用户单击btnChangeIcon却未输入任何信息,则可能会出现问题,因此如果您沿该路线行驶,则应添加一些错误处理。 如果您通过代码进行设置,则可以。

对于没有关联的图标,除了自己为它们创建关联外,我不知道该怎么做。

  • SP

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM