Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Jun 9, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

SetFolderIcon - Associate an icon to a folder

Private Declare Function SetFileAttributes Lib "kernel32" Alias _
    "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As _
    Long) As Long

' Associate an icon to a folder, by creating a desktop.ini file into it.
' Within Explorer the specified icon will be displayed instead of the standard 
' folder icon
' Parameters:
'   sFolderPath: path of the folder to associate the icon to
'   sIconFile: icon file to associate. This can be a .ico, .exe or .dll file
'   iIconIndex: index of the icon in case the specified file contains more than 
' one icon (such as shell32.dll)
' Example:
'   SetFolderIcon "C:\NiceFolder", "C:\Documents\TestIcon.ico"

Sub SetFolderIcon(ByVal sFolderPath As String, ByVal sIconFile As String, _
    Optional ByVal iIconIndex As Long = 0)
    Dim sContent As String
    Dim sDesktopFile As String
    Dim bExists As Boolean
    Dim handle As Integer
    On Error Resume Next
    ' test whether both the folder and the icon file exist
    bExists = (GetAttr(sFolderPath) And vbDirectory) <> 0 And (GetAttr _
        (sIconFile) And vbDirectory) = 0
    If bExists = False Or Err.Number <> 0 Then Exit Sub
    ' build the file content
    sContent = "[.ShellClassInfo]" & vbCrLf & "IconIndex=" & iIconIndex & _
        vbCrLf & "IconFile=" & sIconFile
    'make the path for the desktop.ini file
    sDesktopFile = sFolderPath & IIf(Right$(sFolderPath, 1) = "\", "", _
        "\") & "desktop.ini"
    ' open the file and write the content
    handle = FreeFile
    Open sDesktopFile For Output As #handle
    Print #handle, sContent;
    Close #handle
    ' make the file hidden and the folder read only
    SetFileAttributes sDesktopFile, vbHidden
    SetFileAttributes sFolderPath, vbReadOnly
End Sub
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date