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 7417919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:47:26+00:00 2026-05-29T07:47:26+00:00

I have a great technique to get the associated extension/image in the current system

  • 0

I have a great technique to get the associated extension/image in the current system (because extensions can have different images from a system to another). And here is the function:

public static Icon getIconFromFile(string ext, bool large = true)
{
        string fileName = (new Random()).Next(100, 1000).ToString() + ext;
        System.IO.File.Create(fileName);
        System.Drawing.Icon icon;
        SHFILEINFO shinfo = new SHFILEINFO();

        if (large)
        {
            IntPtr hImgLarge = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
            icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
        }
        else
        {
            IntPtr hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
            icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
        }
        try
        {
            System.IO.File.Delete(fileName);
        }
        catch(Exception e)
        {
            System.Console.WriteLine(e.StackTrace);
        }
        return icon;
    }

The problem is that the function doesn’t close the access to the file, so i can’t remove it. How can i do ? Thanks

  • 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-29T07:47:27+00:00Added an answer on May 29, 2026 at 7:47 am

    File.Create returns a stream that references the file you’ve created. To ensure that the stream is closed properly, you should wrap it in a using block:

    System.Drawing.Icon icon;
    using(var stream = System.IO.File.Create(fileName))
    {
        SHFILEINFO shinfo = new SHFILEINFO();
    
        if (large)
        {
            IntPtr hImgLarge = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
            icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
        }
        else
        {
            IntPtr hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
            icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
        }
    }
    try
    {
        System.IO.File.Delete(fileName);
    }
    catch(Exception e)
    {
        System.Console.WriteLine(e.StackTrace);
    }
    return icon;
    

    It doesn’t matter if you use the returned stream or not, you need to ensure it’s disposed so that the file can be deleted. Note also that I’ve moved the declaration of icon to outside the using block so that you can return it at the end of the method.

    I should also point out that:

    string fileName = (new Random()).Next(100, 1000).ToString() + ext;
    

    Is a bit of a “bad idea” when you could just as easily call “System.IO.Path.GetTempFileName();” and leave creating the file and uniquely naming it to the Operating System, rather than attempting to do that yourself. THat has the added bonus of the file being created in the users temp directory, which is the best place for it given that it’s a temporary file. You’d also then not encounter the issue of the file being held open (there are various other methods like System.IO.Path.GetExtension you could use to rename the file so it has the appropriate extension.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used to have a great Firefox add-on for this and I can't remember
I have a Delphi 6 application that receives and processes an image stream from
I have a great app for capturing shoutcast streams :-) . So far, it
I have a great concern in deploying the TinyMCE editor on a website. Looking
Scenario: We have a great deal of server environmental information (names, IPs, roles, firewall
I have a colleague in my company whose opinions I have a great deal
In my work I have with great results used approximate string matching algorithms such
So I've asked a couple of questions about performing joins and have had great
I'm new to integration testing, but have had great success so far buiding up
I have this working great, but I'd like a deeper understanding of what is

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.