2009-07-28 31 views

回答

0

好吧,把它们放在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
0

在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,所以我不能帮助更多。 (并且可能存在比这里描述的更好的的方式来执行它。)

相关问题