locked
How to change the folder's icon? And how to save the icon as My.Settings? RRS feed

  • Question

  • Hey,

    As this is the first question for me here, so i hope that someone helps me! So, i'm making file/folder protector in VB.Net also, in the same application, i have a drive locker. All of these things work well. What i need is how to change the folder's icon (from the path, for eg: C:\Users\PC\Desktop\Files) So here i wanna change "Files" I have a code but it only works for .EXE Files. Also i've searched and i've found alot of solutions such as making the "desktop.ini" but i don't understand it.

    If .exe code will help then tell me and i will post it.

    Kind Regards,

    John Zack.

    Monday, October 17, 2016 6:21 PM

Answers

  •   You will also need to set the file attributes of the Desktop.ini file and the Folder so they include the System attribute or the Desktop.ini file will not do any good.  Also,  the icon for the folder must be located on the hard drive so,  if it is added to your application`s  resources,  you need to write it to the hard drive.  Placing it inside the folder would work fine.

     You can also set the Hidden attributes to the Desktop.ini file and the icon so that they are not visible in the folder.  Below is an example that works good for me to do all this by calling the SetFolderIcon sub.

    Public Class Form1
        Private myFolder As String = "C:\TestFolder\My Folder"
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Create your folder if it does not exist yet
            If Not IO.Directory.Exists(myFolder) Then IO.Directory.CreateDirectory(myFolder)
    
            'Set the folder Icon and Baloon Tip Text
            SetFolderIcon(myFolder, My.Resources.MyIcon, "My Icon Test Folder")
        End Sub
    
        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 to the folder if it does not exist in the folder. This must be a real icon file (.ico)
                    Dim iconFile As String = IO.Path.Combine(folder, "FolderIcon.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 so it is not seen
                    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=FolderIcon.ico", "IconIndex=0", "InfoTip=" & tip}
                        IO.File.WriteAllLines(iniFile, iniStr)
                    Else
                        Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=FolderIcon.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 set already
                    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.ToString)
                End Try
            End If
        End Sub
    End Class
    
     

     Here you can see that i create a folder named "My Folder" which uses a camera icon that i have added to the apps Resources.


    If you say it can`t be done then i`ll try it

    • Marked as answer by John Zack Tuesday, October 18, 2016 6:19 PM
    Monday, October 17, 2016 7:35 PM
  • Here is an example, you would need to point to the icon file and folder to save the desktop.ini file too. Here I'm using mocked file and folder.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim desktopFile As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Desktop.ini")
            Dim iconFile As String = "C:\SomeFolder\SomeFile.ico"
            Dim theTip As String = "My special tip"
            Dim Template As String =
                <Template>
    [.ShellClassInfo]
    IconFile=<%= iconFile %>
    IconIndex=0
    ConfirmFileOp=0
    InfoTip=<%= theTip %>
                </Template>.Value
    
            IO.File.WriteAllText(desktopFile, Template)
            IO.File.SetAttributes(desktopFile, IO.FileAttributes.Hidden)
        End Sub

    Produces

    [.ShellClassInfo]
    IconFile=C:\SomeFolder\SomeFile.ico
    IconIndex=0
    ConfirmFileOp=0
    InfoTip=My special tip


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, October 17, 2016 7:36 PM
  •   All you need to do is use an OpenFileDialog Class to let the user choose an icon file.  You could also use a FolderBrowserDialog Class to let the user select the folder too.  Below is a modified version of my example that will let you pass the path of an icon to the SetFolderIcon sub instead of an actual Icon image.

     You should be able to modify this to fit your needs.  Also,  now that your original question has been answered,  any additional questions should be asked in a new question.  You can link back to this thread from your new question if needed.

    Public Class Form1
        Private FolderPath As String = ""
        Private IconPath As String = ""
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'have the user select a folder
            Using fb As New FolderBrowserDialog
                fb.Description = "Select a folder to change icon of..."
                fb.ShowNewFolderButton = True
                If fb.ShowDialog = DialogResult.OK Then
                    FolderPath = fb.SelectedPath
                Else
                    Exit Sub
                End If
            End Using
    
            'have the user select an icon file
            Using ofd As New OpenFileDialog
                ofd.Filter = "Icon Files|*.ico"
                If ofd.ShowDialog = DialogResult.OK Then
                    IconPath = ofd.FileName
                Else
                    Exit Sub
                End If
            End Using
    
            'Call the SetFolderIcon sub to set the folder Icon and tooltip text of the selected folder
            SetFolderIcon(FolderPath, IconPath, "My special tooltip text for the folder.")
    
            'Or you can call the SetFolderIcon sub without the tooltip text if you don`t want to set that. The system will use the default tooltip if you don`t.
            'SetFolderIcon(myFolderPath, myIconPath)
        End Sub
    
        Private Sub SetFolderIcon(ByVal FolderPath As String, ByVal IconPath As String, Optional ByVal ToolTipText As String = "")
            If IO.Directory.Exists(FolderPath) Then
                Try
                    'Copy the Icon from the IconPath to the folder you want to change the icon of
                    Dim iconFile As String = IO.Path.Combine(FolderPath, "FolderIcon.ico")
                    IO.File.Copy(IconPath, iconFile)
    
                    'Set the Hidden attribute for the icon file so it is not seen in the folder
                    Dim ifa As New IO.FileInfo(iconFile)
                    ifa.Attributes = ifa.Attributes Or IO.FileAttributes.Hidden
    
                    'Create or Overwrite a Desktop.ini file in the folder. If you passed a tooltip in the (tip) parameter, then it will be added to the file.
                    'if it is not passed in the (tip) parameter, then it is not writen in file and the system will use the default tooltip.
                    Dim iniFile As String = IO.Path.Combine(FolderPath, "Desktop.ini")
                    If ToolTipText <> "" Then
                        Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=FolderIcon.ico", "IconIndex=0", "InfoTip=" & ToolTipText}
                        IO.File.WriteAllLines(iniFile, iniStr)
                    Else
                        Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=FolderIcon.ico", "IconIndex=0"}
                        IO.File.WriteAllLines(iniFile, iniStr)
                    End If
    
                    'Set the System and Hidden file attributes 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 set already
                    Dim di As New IO.DirectoryInfo(FolderPath)
                    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.ToString)
                End Try
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by John Zack Wednesday, October 19, 2016 4:00 PM
    Tuesday, October 18, 2016 10:01 PM

All replies

  • Hi,

    One method is to create a desktop.ini file via code and set the icon there e.g.,

    ini

    [.ShellClassInfo]
    IconFile=\\ASD\DEV\Shared\SandBox\KP\DotNet\wizard.ico
    IconIndex=0
    ConfirmFileOp=0
    InfoTip=My Dotnet files


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by IronRazerz Monday, October 17, 2016 7:04 PM
    Monday, October 17, 2016 6:52 PM
  • Hi,

    One method is to create a desktop.ini file via code and set the icon there e.g.,


    ini

    [.ShellClassInfo]
    IconFile=\\ASD\DEV\Shared\SandBox\KP\DotNet\wizard.ico
    IconIndex=0
    ConfirmFileOp=0
    InfoTip=My Dotnet files


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    So the only way is creating desktop.ini? If yes, then how to make it via that "code" and how to put it in the folder from the folder Path?

    thanks!

    Monday, October 17, 2016 7:04 PM
  •   You will also need to set the file attributes of the Desktop.ini file and the Folder so they include the System attribute or the Desktop.ini file will not do any good.  Also,  the icon for the folder must be located on the hard drive so,  if it is added to your application`s  resources,  you need to write it to the hard drive.  Placing it inside the folder would work fine.

     You can also set the Hidden attributes to the Desktop.ini file and the icon so that they are not visible in the folder.  Below is an example that works good for me to do all this by calling the SetFolderIcon sub.

    Public Class Form1
        Private myFolder As String = "C:\TestFolder\My Folder"
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Create your folder if it does not exist yet
            If Not IO.Directory.Exists(myFolder) Then IO.Directory.CreateDirectory(myFolder)
    
            'Set the folder Icon and Baloon Tip Text
            SetFolderIcon(myFolder, My.Resources.MyIcon, "My Icon Test Folder")
        End Sub
    
        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 to the folder if it does not exist in the folder. This must be a real icon file (.ico)
                    Dim iconFile As String = IO.Path.Combine(folder, "FolderIcon.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 so it is not seen
                    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=FolderIcon.ico", "IconIndex=0", "InfoTip=" & tip}
                        IO.File.WriteAllLines(iniFile, iniStr)
                    Else
                        Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=FolderIcon.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 set already
                    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.ToString)
                End Try
            End If
        End Sub
    End Class
    
     

     Here you can see that i create a folder named "My Folder" which uses a camera icon that i have added to the apps Resources.


    If you say it can`t be done then i`ll try it

    • Marked as answer by John Zack Tuesday, October 18, 2016 6:19 PM
    Monday, October 17, 2016 7:35 PM
  • Here is an example, you would need to point to the icon file and folder to save the desktop.ini file too. Here I'm using mocked file and folder.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim desktopFile As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Desktop.ini")
            Dim iconFile As String = "C:\SomeFolder\SomeFile.ico"
            Dim theTip As String = "My special tip"
            Dim Template As String =
                <Template>
    [.ShellClassInfo]
    IconFile=<%= iconFile %>
    IconIndex=0
    ConfirmFileOp=0
    InfoTip=<%= theTip %>
                </Template>.Value
    
            IO.File.WriteAllText(desktopFile, Template)
            IO.File.SetAttributes(desktopFile, IO.FileAttributes.Hidden)
        End Sub

    Produces

    [.ShellClassInfo]
    IconFile=C:\SomeFolder\SomeFile.ico
    IconIndex=0
    ConfirmFileOp=0
    InfoTip=My special tip


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, October 17, 2016 7:36 PM
  • Thanks, all. I will try it tomorrow, because i've to close now!

    cya and thanks again

    Monday, October 17, 2016 7:40 PM
  • Okay, look, i want it to be done by getting the folder path from txtPath.text, and then changes the folder path not to create another folder..
    • Edited by John Zack Tuesday, October 18, 2016 3:45 PM
    Tuesday, October 18, 2016 3:43 PM
  •  Well,  it is very easy to just use your folder path in place of the path i used.  The only reason i create the folder if it does not exist before setting the icon is so that i don`t have to create it in Explorer before running the example code.  It is just an example and i figured you would understand how to change it slightly to fit your needs.  All you need to do is call the SetFolderIcon sub using the txtPath.Text path.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Set the folder Icon and Baloon Tip Text
            SetFolderIcon(txtPath.Text, My.Resources.MyIcon, "My Icon Test Folder")
        End Sub

     

     Edit:  I should also add that some of the common folders that already have a special icon such as the Documents,  Videos,  Pictures,  and other folders, will already have a hidden Desktop.ini file in them.  If you set the icons of those folders,  you better back up the existing Desktop.ini file before you overwrite the Desktop.ini file.  If you don`t,  you are not going to be able to set it back to it`s original icon.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, October 18, 2016 5:43 PM
    Tuesday, October 18, 2016 5:23 PM
  • Okay, thanks guys, it worked! Now another question but the same idea, it's about icon too.

    I want to choose the icon from the open file dialog with a text box , is it possible?

    Tuesday, October 18, 2016 6:33 PM
  • So? Any help?
    Tuesday, October 18, 2016 7:11 PM
  •   All you need to do is use an OpenFileDialog Class to let the user choose an icon file.  You could also use a FolderBrowserDialog Class to let the user select the folder too.  Below is a modified version of my example that will let you pass the path of an icon to the SetFolderIcon sub instead of an actual Icon image.

     You should be able to modify this to fit your needs.  Also,  now that your original question has been answered,  any additional questions should be asked in a new question.  You can link back to this thread from your new question if needed.

    Public Class Form1
        Private FolderPath As String = ""
        Private IconPath As String = ""
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'have the user select a folder
            Using fb As New FolderBrowserDialog
                fb.Description = "Select a folder to change icon of..."
                fb.ShowNewFolderButton = True
                If fb.ShowDialog = DialogResult.OK Then
                    FolderPath = fb.SelectedPath
                Else
                    Exit Sub
                End If
            End Using
    
            'have the user select an icon file
            Using ofd As New OpenFileDialog
                ofd.Filter = "Icon Files|*.ico"
                If ofd.ShowDialog = DialogResult.OK Then
                    IconPath = ofd.FileName
                Else
                    Exit Sub
                End If
            End Using
    
            'Call the SetFolderIcon sub to set the folder Icon and tooltip text of the selected folder
            SetFolderIcon(FolderPath, IconPath, "My special tooltip text for the folder.")
    
            'Or you can call the SetFolderIcon sub without the tooltip text if you don`t want to set that. The system will use the default tooltip if you don`t.
            'SetFolderIcon(myFolderPath, myIconPath)
        End Sub
    
        Private Sub SetFolderIcon(ByVal FolderPath As String, ByVal IconPath As String, Optional ByVal ToolTipText As String = "")
            If IO.Directory.Exists(FolderPath) Then
                Try
                    'Copy the Icon from the IconPath to the folder you want to change the icon of
                    Dim iconFile As String = IO.Path.Combine(FolderPath, "FolderIcon.ico")
                    IO.File.Copy(IconPath, iconFile)
    
                    'Set the Hidden attribute for the icon file so it is not seen in the folder
                    Dim ifa As New IO.FileInfo(iconFile)
                    ifa.Attributes = ifa.Attributes Or IO.FileAttributes.Hidden
    
                    'Create or Overwrite a Desktop.ini file in the folder. If you passed a tooltip in the (tip) parameter, then it will be added to the file.
                    'if it is not passed in the (tip) parameter, then it is not writen in file and the system will use the default tooltip.
                    Dim iniFile As String = IO.Path.Combine(FolderPath, "Desktop.ini")
                    If ToolTipText <> "" Then
                        Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=FolderIcon.ico", "IconIndex=0", "InfoTip=" & ToolTipText}
                        IO.File.WriteAllLines(iniFile, iniStr)
                    Else
                        Dim iniStr() As String = {"[.ShellClassInfo]", "IconFile=FolderIcon.ico", "IconIndex=0"}
                        IO.File.WriteAllLines(iniFile, iniStr)
                    End If
    
                    'Set the System and Hidden file attributes 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 set already
                    Dim di As New IO.DirectoryInfo(FolderPath)
                    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.ToString)
                End Try
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by John Zack Wednesday, October 19, 2016 4:00 PM
    Tuesday, October 18, 2016 10:01 PM
  • Thanks, as i said, the question is the same so no need to create a new thread + I've marked the answers for the half of my question, now i will try it and will tell ya!

    Thanks!!

    Wednesday, October 19, 2016 3:28 PM
  • Thanks, it worked.

    Have a nice day!

    Wednesday, October 19, 2016 4:00 PM