如何更改我使用 VB 创建的文件夹的文件资源管理器图标
How do I change the File Explorer icon for a folder I'm creating with VB
在我的 Visual Basic 处理中,我正在创建许多文件夹。有没有办法,使用 VB 代码,我可以更改文件资源管理器中为这些文件夹显示的图标?我想我必须在使用 My.Computer.FileSystem.CreateDirectory 时更改 属性,但我不确定该怎么做。
你可以这样做:
Private Sub SetFolderIcon(ByVal folder As String, ByVal ico As Icon, Optional ByVal tip As String = "")
If IO.Directory.Exists(folder) Then
Try
'Copy the icon from the resources into the project folder if it doesn't exist
Dim iconFile As String = IO.Path.Combine(folder, "Icon.ico")
If Not IO.File.Exists(iconFile) Then
Using st As New IO.FileStream(iconFile, IO.FileMode.Create)
ico.Save(st)
End Using
End If
'Set the hidden attribute for the icon file to be invisible
Dim ifa As New IO.FileInfo(iconFile)
ifa.Attributes = ifa.Attributes Or IO.FileAttributes.Hidden
'Create a new Desktop.ini file in the folder
Dim iniFile As String = IO.Path.Combine(folder, "Desktop.ini")
If tip <> "" Then
Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=Icon.ico", "IconIndex=0", "InfoTip=" & tip}
IO.File.WriteAllLines(iniFile, iniStr)
Else
Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=Icon.ico", "IconIndex=0"}
IO.File.WriteAllLines(iniFile, iniStr)
End If
'Set the system and hidden file attribute for the new Desktop.ini file
Dim fa As New IO.FileInfo(iniFile)
fa.Attributes = fa.Attributes Or IO.FileAttributes.System Or IO.FileAttributes.Hidden
'Set the system attribute for the folder if it is not already set
Dim di As New IO.DirectoryInfo(folder)
If (di.Attributes And IO.FileAttributes.System) <> IO.FileAttributes.System Then
di.Attributes = di.Attributes Or IO.FileAttributes.System
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End If
End Sub
然后运行上面的代码:
SetFolderIcon(folder, My.Resources.icon, "Custom folder")
在我的 Visual Basic 处理中,我正在创建许多文件夹。有没有办法,使用 VB 代码,我可以更改文件资源管理器中为这些文件夹显示的图标?我想我必须在使用 My.Computer.FileSystem.CreateDirectory 时更改 属性,但我不确定该怎么做。
你可以这样做:
Private Sub SetFolderIcon(ByVal folder As String, ByVal ico As Icon, Optional ByVal tip As String = "")
If IO.Directory.Exists(folder) Then
Try
'Copy the icon from the resources into the project folder if it doesn't exist
Dim iconFile As String = IO.Path.Combine(folder, "Icon.ico")
If Not IO.File.Exists(iconFile) Then
Using st As New IO.FileStream(iconFile, IO.FileMode.Create)
ico.Save(st)
End Using
End If
'Set the hidden attribute for the icon file to be invisible
Dim ifa As New IO.FileInfo(iconFile)
ifa.Attributes = ifa.Attributes Or IO.FileAttributes.Hidden
'Create a new Desktop.ini file in the folder
Dim iniFile As String = IO.Path.Combine(folder, "Desktop.ini")
If tip <> "" Then
Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=Icon.ico", "IconIndex=0", "InfoTip=" & tip}
IO.File.WriteAllLines(iniFile, iniStr)
Else
Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=Icon.ico", "IconIndex=0"}
IO.File.WriteAllLines(iniFile, iniStr)
End If
'Set the system and hidden file attribute for the new Desktop.ini file
Dim fa As New IO.FileInfo(iniFile)
fa.Attributes = fa.Attributes Or IO.FileAttributes.System Or IO.FileAttributes.Hidden
'Set the system attribute for the folder if it is not already set
Dim di As New IO.DirectoryInfo(folder)
If (di.Attributes And IO.FileAttributes.System) <> IO.FileAttributes.System Then
di.Attributes = di.Attributes Or IO.FileAttributes.System
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End If
End Sub
然后运行上面的代码:
SetFolderIcon(folder, My.Resources.icon, "Custom folder")