programatically change icon on folder

Hi Experts,

I have a program where the user creates a project consisting of a Access database and some pictures. I collect all files for a project in a folder and want it to look a little bit more professional by changing the icon of the folder to my own. How do I do that? I want to assign my own icon to the folder when I create it...

Who is Participating?
FER_GConnect With a Mentor Commented:
PerryDaynac; I put the code that you just need into the code snippet, so take a look and prove it!

Ing. Fernando D. Giletta
San Fco. Cba. Argentina.
Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
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
    'if folder and 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
    ' file content
    sContent = "[.ShellClassInfo]" & vbCrLf & "IconFile=" & sIconFile & _
        vbCrLf & "IconIndex=" & iIconIndex
    'path of "desktop.ini" file
    sDesktopFile = sFolderPath & IIf(Right$(sFolderPath, 1) = "\", "", "\") & "Desktop.ini"
    'change the file attr to delete it if it already exist
    SetFileAttributes sDesktopFile, vbNormal 'Se setea a visible el estado del archivo para poder eliminarolo.
    If (Dir(sDesktopFile) <> "") Then Kill (sDesktopFile)
    ' 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
    MsgBox "ICON SET!!!"
End Sub

Open in new window

I've found this example but it's in C#.
Say something if you need help converting to
PerryDaynac; you have to add a file "Desktop.ini" into the folder that you want to change the icon
Then add the next code to that file ...


close and save the file "Desktop.ini" and your folder will have the icon that you deside.
All of this, you can do it with VB...

Ing. Fernando D. Giletta
San Fco. Cba. Argentina.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

PerryDaynacAuthor Commented:
Brilliant Fernando!!
Works like a clock...
I have another question up about protecting the folder with a password, if you know how to do that aswell
Thank You
It doesn't like programmatical setting of system file attributes, but you can use SyncShell (or if you don't want to go find SyncShell on the web, just plain old "Shell") to do this quite easily. This will set the attributes of Desktop.ini to both "system" and "hidden"

It's not necessary to set the folder path to read only, btw?

    SyncShell "cmd.exe /c attrib """ & sFolderPath & "\Desktop.ini"" +s +h", vbHide
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.