Registry path to icon resource

Heiko 1,256 Reputation points
2021-07-21T14:52:46.243+00:00

I have virtual files in my app. I would like to display the associated icon for these. In the registry I find the following target under various paths:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\VCSPackages\csproj.dll,1

However, this DLL does not contain the icon that Windows Explorer displays for a CS file.

Under which registry path can I find the entry to the file from which I can extract the CS icon?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,777 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,936 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,734 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,211 Reputation points
    2021-07-21T15:10:47.227+00:00

    If you want to extract the icon from an extension, you can do :

    SHFILEINFO sfi = new SHFILEINFO();
    IntPtr hImageList = SHGetFileInfo(".cs", 0, ref sfi, (uint)Marshal.SizeOf(sfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES);
    Icon ico = Icon.FromHandle(sfi.hIcon);
    // Test displaying icon on desktop
    using (Graphics gr = Graphics.FromHwnd(IntPtr.Zero))
    {
        gr.DrawIcon(ico, 50, 50);
    }
    

    with declarations :

    public const int SHGFI_ICON = 0x100;  // Get icon
    public const int SHGFI_DISPLAYNAME = 0x200;  // Get display name
    public const int SHGFI_TYPENAME = 0x400;  // Get type name
    public const int SHGFI_ATTRIBUTES = 0x800;  // Get attributes
    public const int SHGFI_ICONLOCATION = 0x1000;  // Get icon location
    public const int SHGFI_EXETYPE = 0x2000;  // Return exe type
    public const int SHGFI_SYSICONINDEX = 0x4000;  // Get system icon index
    public const int SHGFI_LINKOVERLAY = 0x8000;  // put a link overlay On icon
    public const int SHGFI_SELECTED = 0x10000;  // Show() icon In selected state
    public const int SHGFI_ATTR_SPECIFIED = 0x20000; // Get only specified attributes
    
    public const int SHGFI_LARGEICON = 0x0; // Get large icon
    public const int SHGFI_SMALLICON = 0x1; // Get small icon
    public const int SHGFI_OPENICON = 0x2; // Get open icon
    public const int SHGFI_SHELLICONSIZE = 0x4; // Get shell size icon
    public const int SHGFI_PIDL = 0x8; // pszPath Is a pidl
    public const int SHGFI_USEFILEATTRIBUTES = 0x10; // use passed dwFileAttribute
    public const int SHGFI_ADDOVERLAYS = 0x20; // apply the appropriate overlays
    public const int SHGFI_OVERLAYINDEX = 0x40; // Get the index Of the overlay in the upper 8 bits of the iIcon
    
    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
        public IntPtr hIcon;
        public int iIcon;
        public int dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    }
    
    [DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
    

2 additional answers

Sort by: Most helpful
  1. RLWA32 45,571 Reputation points
    2021-07-21T14:58:52.55+00:00

    Have you considered using the Visual Studio Image Library?


  2. Heiko 1,256 Reputation points
    2021-07-21T16:37:07.5+00:00

    @RLWA32 : There are nicer icons:

    116788-explorer.png

    The txt icon is from EditPlus, my favorite (text) editor.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.