5 Replies - 3683 Views - Last Post: 25 December 2012 - 02:33 AM Rate Topic: -----

#1 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 09-October 12

Changing folder icon in Windows 7 problem.

Posted 23 December 2012 - 07:45 AM

Hello guys,

I have a problem,
i am currently creating an application for managing windows 7 styles in folder.

I have programmed everything but left the basic icon changer code for the end(big mistake).

What i want to do is to change the icon of a specified folder.

What i am trying is giving me an error saying that access to the file is denied. I am trying to edit the desktop.ini inside the folder.
My code is:
    Public Shared Sub ApplyStyle(ByVal Query As String)
        Dim Info As String() = Query.Split("|")
        Dim Name As String = Info(0)
        Dim Path As String = Info(1)
        Dim Icon As String = Info(2)
        If IO.File.Exists(Icon) = True Then
            If IO.Directory.Exists(Path) = True Then
                Dim Reader As New IO.StreamReader(Path & "\desktop.ini")
                Dim Data As New RichTextBox
                Data.Text = Reader.ReadToEnd
                Reader.ReadToEnd()
                If Data.Text.Contains("IconResource=") = True Then
                    Dim Line As Integer = Data.GetLineFromCharIndex(Data.Text.IndexOf("IconResource=")) ' 13
                    Dim Lines As String() = Data.Lines
                    Lines(Line) = "IconResource=" & Icon & ",0"
                    Data.Lines = Lines
                Else
                    Data.Text = "[.ShellClassInfo]" & vbNewLine & _
                                "IconResource=" & Icon & ",0" & vbNewLine & _
                                Data.Text
                End If
                Dim Writer As New IO.StreamWriter(Path & "\desktop.ini")
                Writer.Write(Data.Text)
                Writer.Dispose()
            Else
                MsgBox("Selected folder doesn't exist, please edit the preferences of the style to fix the problem.", MsgBoxStyle.Critical, "Error")
            End If
            Else
                MsgBox("Icon doesn't exists, unable to enable the style. Please edit the style preferences.", MsgBoxStyle.Critical, "Error")
            End If
    End Sub



Please help me because i need the app finished in a few weeks.

Is This A Good Question/Topic? 0
  • +

Replies To: Changing folder icon in Windows 7 problem.

#2 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Changing folder icon in Windows 7 problem.

Posted 23 December 2012 - 08:56 AM

Quote

Access to the file is denied...


Have you tried running your app using admin privileges?
Was This Post Helpful? 0
  • +
  • -

#3 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 142
  • Joined: 19-May 12

Re: Changing folder icon in Windows 7 problem.

Posted 23 December 2012 - 11:46 AM

Firstly Import these things
Imports System.Security.AccessControl
Imports System.IO


Call this sub right before changing the icon
Public Sub AllowFolderAccessPermissionForAllUsers(ByVal FolderPath As String)
        Dim fs As FileSystemSecurity = File.GetAccessControl(FolderPath)
        fs.RemoveAccessRule(New FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow))
        File.SetAccessControl(FolderPath, fs)
End Sub


If this does not work then Replace the word "Allow" by "Deny"

this will allow you to have folder access permissions
Actually this is not coded by me but I found it on microsoft site

I have tested this code before some weeks and it worked for me but I forgot that I have used the word "Allow" or "Deny"

This post has been edited by Vishal1419: 23 December 2012 - 11:47 AM

Was This Post Helpful? -1
  • +
  • -

#4 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 09-October 12

Re: Changing folder icon in Windows 7 problem.

Posted 24 December 2012 - 12:15 AM

View PostBlueMelon, on 23 December 2012 - 08:56 AM, said:

Quote

Access to the file is denied...

Yes and it doesn't prevent the error from returning again.
Have you tried running your app using admin privileges?


View PostVishal1419, on 23 December 2012 - 11:46 AM, said:

Firstly Import these things
Imports System.Security.AccessControl
Imports System.IO


Call this sub right before changing the icon
Public Sub AllowFolderAccessPermissionForAllUsers(ByVal FolderPath As String)
        Dim fs As FileSystemSecurity = File.GetAccessControl(FolderPath)
        fs.RemoveAccessRule(New FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow))
        File.SetAccessControl(FolderPath, fs)
End Sub


If this does not work then Replace the word "Allow" by "Deny"

this will allow you to have folder access permissions
Actually this is not coded by me but I found it on microsoft site

I have tested this code before some weeks and it worked for me but I forgot that I have used the word "Allow" or "Deny"

I tried both .Allow and .Deny, but it doesn't work. I am still unable to save the desktop.ini.
Full error details:

System.UnauthorizedAccessException was unhandled
  HResult=-2147024891
  Message=Access to the path 'C:\Users\Admin\Desktop\Apps\desktop.ini' is denied.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
       at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
       at System.IO.StreamWriter..ctor(String path)
       at Windows_7_Styler.Functions.ApplyStyle(String Query) in C:\Users\Admin\Documents\Visual Studio 2010\Projects\Windows 7 Styler\Windows 7 Styler\Functions.vb:line 144
       at Windows_7_Styler.StylesBrowser.Browser_ItemCheck(Object sender, EventArgs e) in C:\Users\Admin\Documents\Visual Studio 2010\Projects\Windows 7 Styler\Windows 7 Styler\StylesBrowser.vb:line 46
       at System.Windows.Forms.CheckedListBox.OnItemCheck(ItemCheckEventArgs ice)
       at System.Windows.Forms.CheckedListBox.LbnSelChange()
       at System.Windows.Forms.CheckedListBox.WmReflectCommand(Message& m)
       at System.Windows.Forms.ListBox.WndProc(Message& m)
       at System.Windows.Forms.CheckedListBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WmCommand(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Nativewindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ListBox.WndProc(Message& m)
       at System.Windows.Forms.CheckedListBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Windows_7_Styler.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 



Was This Post Helpful? 0
  • +
  • -

#5 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 142
  • Joined: 19-May 12

Re: Changing folder icon in Windows 7 problem.

Posted 24 December 2012 - 01:18 AM

sorry to say but I dont dont know more about that
Was This Post Helpful? 0
  • +
  • -

#6 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 09-October 12

Re: Changing folder icon in Windows 7 problem.

Posted 25 December 2012 - 02:33 AM

Hello guys, just posted to let you know that i found the solution to the problem.
I used these two files as imports in my project and solved it.

Foldericon.vb
Imports System.IO

Namespace FolderIcons
    Public Class FolderIcon
        Private m_folderPath As String = ""
        Private m_iniPath As String = ""
        Public Sub New(ByVal folderPath As String)
            Me.FolderPath = folderPath
        End Sub

        Public Sub CreateFolderIcon(ByVal iconFilePath As String, ByVal infoTip As String)
            If CreateFolder() Then
                CreateDesktopIniFile(iconFilePath, infoTip)
                SetIniFileAttributes()
                SetFolderAttributes()
            End If
        End Sub

        Public Sub CreateFolderIcon(ByVal targetFolderPath As String, ByVal iconFilePath As String, ByVal infoTip As String)
            Me.FolderPath = targetFolderPath
            Me.CreateFolderIcon(iconFilePath, infoTip)
        End Sub


        Public Property FolderPath() As String
            Get
                Return Me.m_folderPath
            End Get
            Set(ByVal value As String)
                m_folderPath = value
                If Not Me.m_folderPath.EndsWith("\") Then
                    Me.m_folderPath += "\"
                End If
            End Set
        End Property

        Public Property IniPath() As String
            Get
                Return m_iniPath
            End Get
            Set(ByVal value As String)
                m_iniPath = value
            End Set
        End Property

        Private Function CreateFolder() As Boolean

            If Me.FolderPath.Length = 0 Then
                Return False
            End If


            If Directory.Exists(Me.FolderPath) Then
                Return True
            End If

            Try

                Dim di As DirectoryInfo = Directory.CreateDirectory(Me.FolderPath)
            Catch e As Exception
                Return False
            End Try

            Return True
        End Function

        Private Function CreateDesktopIniFile(ByVal iconFilePath As String, ByVal getIconFromDLL As Boolean, ByVal iconIndex As Integer, ByVal infoTip As String) As Boolean

            If Not Directory.Exists(Me.FolderPath) Then
                Return False
            End If


            If Not File.Exists(iconFilePath) Then
                Return False
            End If

            If Not getIconFromDLL Then
                iconIndex = 0
            End If


            Me.IniPath = Me.FolderPath & "desktop.ini"


            IniWriter.WriteValue(".ShellClassInfo", "IconFile", iconFilePath, Me.IniPath)
            IniWriter.WriteValue(".ShellClassInfo", "IconIndex", iconIndex.ToString(), Me.IniPath)
            IniWriter.WriteValue(".ShellClassInfo", "InfoTip", infoTip, Me.IniPath)

            Return True
        End Function

        Private Sub CreateDesktopIniFile(ByVal iconFilePath As String, ByVal infoTip As String)
            Me.CreateDesktopIniFile(iconFilePath, False, 0, infoTip)
        End Sub

        Private Function SetIniFileAttributes() As Boolean

            If Not File.Exists(Me.IniPath) Then
                Return False
            End If


            If (File.GetAttributes(Me.IniPath) And FileAttributes.Hidden) <> FileAttributes.Hidden Then
                File.SetAttributes(Me.IniPath, File.GetAttributes(Me.IniPath) Or FileAttributes.Hidden)
            End If


            If (File.GetAttributes(Me.IniPath) And FileAttributes.System) <> FileAttributes.System Then
                File.SetAttributes(Me.IniPath, File.GetAttributes(Me.IniPath) Or FileAttributes.System)
            End If

            Return True

        End Function

        Private Function SetFolderAttributes() As Boolean

            If Not Directory.Exists(Me.FolderPath) Then
                Return False
            End If

            If (File.GetAttributes(Me.FolderPath) And FileAttributes.System) <> FileAttributes.System Then
                File.SetAttributes(Me.FolderPath, File.GetAttributes(Me.FolderPath) Or FileAttributes.System)
            End If

            Return True

        End Function

    End Class
End Namespace



and

Iniwriter.vb
Imports System.Runtime.InteropServices

Namespace FolderIcons

    Public Class IniWriter

        <DllImport("kernel32")> _
        Private Shared Function WritePrivateProfileString(ByVal iniSection As String, ByVal iniKey As String, ByVal iniValue As String, ByVal iniFilePath As String) As Integer
        End Function

        Public Shared Sub WriteValue(ByVal iniSection As String, ByVal iniKey As String, ByVal iniValue As String, ByVal iniFilePath As String)
            WritePrivateProfileString(iniSection, iniKey, iniValue, iniFilePath)
        End Sub

    End Class
End Namespace




Now in order to use it you need to imports these into your main code:
Imports YourProjectName.FolderIcons



and you need to use this code in order to change the icon of a folder.

Dim myFolderIcon As New FolderIcon("C:\Folder")
myFolderIcon.CreateFolderIcon("C:\Icon.ico", "manos364")
myFolderIcon = Nothing



PS:
The icon will take some time to take place into the folder, so please try closing all explorer windows or restarting explorer if you need it done quickly.

This code doesn't belong to me, all credits go to Ahmed Karim (MoDy kareem)
VB 2008/2010 - Folder Icon Changer - Visual Basic (VB) - MoDy kareem

Please thumb up.
:rockon:/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1