-4

I have many folders with custom icon ( any icon except default windows folder icon) I want to find path of icons associated to a folder directory.

For example I have a folder named "Documents" that has a custom icon in a different directory path. assume that I just have the folder directory path and I want to find the icon's path.

d:\...\customicon.ico  (icon's path)
d:\...\Documents (folder's path)

below is signature of what I want.

string getIconPath(string folderPath) {
    //return associated icon's path
}
  • 3
    You want us to find paths on your PC...? – MarioDS Oct 05 '14 at 18:47
  • no i need to know how to find a folder icon path in c# , like i have folder path : C:\...\a that has a customized icon that i set before from D:\....\sdfa.ico, and i need a function like string geticonpath(@"c:\...\a") that gives me "D:\...\sdfa.ico" – user4111079 Oct 05 '14 at 19:33
  • Can't you store the location at the moment you are setting the image and later use the information? – keenthinker Oct 05 '14 at 21:03

2 Answers2

1

here is the function that i wanted:

string getIconPath(string folderPath) {
    SHFILEINFO shinfo = new SHFILEINFO();
    Win32.SHGetFileInfo(folderPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)0x1000);
    return shinfo.szDisplayName
}

and here are the SHFILEINFO struct and Win32 class implementations:

[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
    public IntPtr hIcon;
    public int iIcon;
    public uint dwAttributes;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public string szDisplayName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    public string szTypeName;
};

class Win32
{
    [DllImport("shell32.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
0

I am not sure, that once you set the icon to a directory, the original image path information is kept somewhere and/or somehow. If you want to extract the icon from the directory, have a look at this SO post and this CodeProject article.

This MSDN article shows how to extract the icon from a directory. One possible solution would be to extract the associated icon from the directory and save it in the directory under the desired name. This way you will have at least the icon again as a file in the desired directory (if that helps).

Community
  • 1
  • 1
keenthinker
  • 7,645
  • 2
  • 35
  • 45
  • no i need to know how to find a folder icon path in c# , like i have folder path : C:\...\a that has a customized icon that i set before from D:\....\sdfa.ico, and i need a function like string geticonpath(@"c:\...\a") that gives me "D:\...\sdfa.ico" – user4111079 Oct 05 '14 at 19:47