Solved

programatically change icon on folder

Posted on 2008-10-16
6
506 Views
Last Modified: 2012-08-13
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...

Thanks
Perry
0
Comment
Question by:PerryDaynac
6 Comments
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22730195
I've found this example but it's in C#.
http://www.codeproject.com/KB/files/foldericonsincsharp.aspx
Say something if you need help converting to vb.net
0
 
LVL 7

Expert Comment

by:FER_G
ID: 22730386
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 ...

[.ShellClassInfo]
IconFile=c:\someIcon.ico
IconIndex=0

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...

FerG
Saludos.
--
Ing. Fernando D. Giletta
San Fco. Cba. Argentina.
0
 
LVL 7

Accepted Solution

by:
FER_G earned 250 total points
ID: 22730647
PerryDaynac; I put the code that you just need into the code snippet, so take a look and prove it!

FerG
Saludos.
--
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

by:PerryDaynac
ID: 31506658
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
0
 
LVL 4

Expert Comment

by:WernerVonBraun
ID: 23722144
Excellent
0
 
LVL 4

Expert Comment

by:WernerVonBraun
ID: 23722377
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now