Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3301206
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:43:47+00:00 2026-05-17T20:43:47+00:00

I need tree file at my asp.net site. For getting icons I try use

  • 0

I need tree file at my asp.net site.
For getting icons I try use SHGetFileInfo api function.
At non asp.net application it works well, returns corrects Icon. When I conusme it at asp.net context I got:

Attempted to read or write protected memory.

What’s wrong? Can I get fiel icon at asp.net context?

Code:

 public class ExtractIcon
{
    [DllImport("Shell32.dll")]
    private static extern int SHGetFileInfo(
    string pszPath,
    uint dwFileAttributes,
    out SHFILEINFO psfi,
    uint cbfileInfo,
    uint uFlags
    );

    [StructLayout(LayoutKind.Sequential)]
    private struct SHFILEINFO
    {
        public SHFILEINFO(bool b)
        {
            hIcon = IntPtr.Zero;
            iIcon = 0;
            dwAttributes = 0;
            szDisplayName = String.Empty;
            szTypeName = String.Empty;
        }

        public IntPtr hIcon;
        public int iIcon;
        public uint dwAttributes;

        [MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
        public string szDisplayName;

        [MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
        public string szTypeName;
    };

    public const uint SHGFI_ICON = 0x000000100; // get icon
    public const uint SHGFI_DISPLAYNAME = 0x000000200; // get display name
    public const uint SHGFI_TYPENAME = 0x000000400; // get type name
    public const uint SHGFI_ATTRIBUTES = 0x000000800; // get attributes
    public const uint SHGFI_ICONLOCATION = 0x000001000; // get icon location
    public const uint SHGFI_EXETYPE = 0x000002000; // return exe type
    public const uint SHGFI_SYSICONINDEX = 0x000004000; // get system icon index
    public const uint SHGFI_LINKOVERLAY = 0x000008000; // put a link overlay on icon
    public const uint SHGFI_SELECTED = 0x000010000; // show icon in selected state
    public const uint SHGFI_ATTR_SPECIFIED = 0x000020000; // get only specified attributes
    public const uint SHGFI_LARGEICON = 0x000000000; // get large icon
    public const uint SHGFI_SMALLICON = 0x000000001; // get small icon
    public const uint SHGFI_OPENICON = 0x000000002; // get open icon
    public const uint SHGFI_SHELLICONSIZE = 0x000000004; // get shell size icon
    public const uint SHGFI_PIDL = 0x000000008; // pszPath is a pidl
    public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010; // use passed dwFileAttribute
    public const uint SHGFI_ADDOVERLAYS = 0x000000020;
    public const uint SHGFI_OVERLAYINDEX = 0x000000040;

    private static Icon GetIcon(string strPath, uint flags)
    {
        SHFILEINFO info = new SHFILEINFO(true);
        int cbFileInfo = Marshal.SizeOf(info);

        SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
        return Icon.FromHandle(info.hIcon);
    }

    /// <summary>
    /// Get the associated Icon for a file or application, this method always returns
    /// an icon. If the strPath is invalid or there is no idonc the default icon is returned
    /// </summary>
    /// <param name="strPath">full path to the file</param>
    /// <param name="bSmall">if true, the 16x16 icon is returned otherwise the 32x32</param>
    /// <returns></returns>
    public static Icon GetIcon(string strPath, bool bSmall)
    {
        uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
        flags |= bSmall ? SHGFI_SMALLICON : SHGFI_LARGEICON;
        return GetIcon(strPath, flags);
    }

    public static Icon GetIcon(string strPath, bool bSmall, bool bOpened)
    {
        uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
        flags |= bSmall ? SHGFI_SMALLICON : SHGFI_LARGEICON;
        flags |= bOpened ? SHGFI_OPENICON : 0;
        return GetIcon(strPath, flags);
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:43:47+00:00Added an answer on May 17, 2026 at 8:43 pm

    The P/Invoke signature you are using for SHGetFileInfo is wrong – use the one at PInvoke.net.

    SHGetFileInfo returns a value that you need to check for success before you access the returned data. If the API call fails the results of accessing the Icon you expected are unpredictable (and usually bad).

    Once you know what the error is, you can work on fixing that as a separate problem. You should never omit error checking code when P/Invoke-ing the Win32 APIs from managed code. Read the MSDN docs for what indicates success and what errors to expect.

    In your case:

        SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
        return Icon.FromHandle(info.hIcon);
    

    should look like this

        IntPtr result = SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
        if (result != IntPtr.Zero)
        {
          return Icon.FromHandle(info.hIcon);
        }
        else
        {
          // add error handling here
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a nodes tree from data.xml file (as here link text
Hey everyone, I am trying to ( temporarily! ) do some ASP.NET and C#
Hey Im working on a folder tree and need to be able to limit
I need to avoid creating double branches in an xml tree when parsing a
Ok so, i have this site that i am putting together in the play
I am aware of using .gitignore file to exclude some files being added, but
I have a Subversion working copy with at least one missing file (the local
Hi all i have written a code to move a file from the original
I am trying to use Google's Sketchup C++ SDK (latest version) to export a
For a huge number of huge csv files (100M lines+) from different sources I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.