473,388 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Retrieve File Icon from Registry

Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin
Nov 16 '05 #1
6 7556
Applications normally store the icon in their exe file, not in the registry.
If the application has stored its path in the registry, and if it does store
an icon in its file that you can get out, you may be able to find its exe
file and extract the icon. But that's quite a lot of "if"s and "might"s...
"mphanke" wrote:
Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin

Nov 16 '05 #2
Okay,

thanks for all replies, finally I got this:
Reuse it if you like it!

#region ExtractAssociatedIcon
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hinst, string file,
ref int index);

public static System.Drawing.Icon FindAssocIcon(string filename)
{
try
{
string ext = System.IO.Path.GetExtension(filename);

RegWin32.RegistryKey key =
RegWin32.Registry.ClassesRoot.OpenSubKey(ext);

if(key != null)
{
string val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
key = RegWin32.Registry.ClassesRoot.OpenSubKey(val);

if(key != null)
{
key = key.OpenSubKey("DefaultIcon");

if(key != null)
{
val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
string name;
int icoIndex = -1;
int index = val.LastIndexOf(',');

if(index > 0)
{
name = val.Substring(0, index);

if(index < val.Length-1)
{
icoIndex = Convert.ToInt32( val.Substring(index+1) );
}
}//index
else
name = val;

System.Reflection.Module [] mod =
System.Reflection.Assembly.GetCallingAssembly().Ge tModules();
IntPtr ptr =
ExtractAssociatedIcon(Marshal.GetHINSTANCE(mod[0]), name, ref icoIndex);

if(ptr != IntPtr.Zero)
{
return System.Drawing.Icon.FromHandle(ptr);
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message+"\n"+ex.InnerException) ;
}
return null;
}
#endregion
mphanke wrote:
Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin

Nov 16 '05 #3
mphanke,

I would recommend that you use the SHGetFileInfo API function, as it
will take into account other things, such as shell extensions and the like,
which ExtractAssociatedIcon does not.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mphanke" <mp*****@nospam.nospam> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
Okay,

thanks for all replies, finally I got this:
Reuse it if you like it!

#region ExtractAssociatedIcon
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hinst, string file, ref
int index);

public static System.Drawing.Icon FindAssocIcon(string filename)
{
try
{
string ext = System.IO.Path.GetExtension(filename);

RegWin32.RegistryKey key = RegWin32.Registry.ClassesRoot.OpenSubKey(ext);

if(key != null)
{
string val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
key = RegWin32.Registry.ClassesRoot.OpenSubKey(val);

if(key != null)
{
key = key.OpenSubKey("DefaultIcon");

if(key != null)
{
val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
string name;
int icoIndex = -1;
int index = val.LastIndexOf(',');

if(index > 0)
{
name = val.Substring(0, index);

if(index < val.Length-1)
{
icoIndex = Convert.ToInt32( val.Substring(index+1) );
}
}//index
else
name = val;

System.Reflection.Module [] mod =
System.Reflection.Assembly.GetCallingAssembly().Ge tModules();
IntPtr ptr = ExtractAssociatedIcon(Marshal.GetHINSTANCE(mod[0]), name, ref
icoIndex);

if(ptr != IntPtr.Zero)
{
return System.Drawing.Icon.FromHandle(ptr);
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message+"\n"+ex.InnerException) ;
}
return null;
}
#endregion
mphanke wrote:
Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin

Nov 16 '05 #4
Hi Martin,

Thanks for posting your code here. I'd like to know if this issue has been
resolved yet. Is there anything that I can help. I'm still monitoring on
it. If you have any questions, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #5
Hi,

with the help of community members this issue was resolved yesterday ;-)

Thanks to all,

Martin

Kevin Yu [MSFT] wrote:
Hi Martin,

Thanks for posting your code here. I'd like to know if this issue has been
resolved yet. Is there anything that I can help. I'm still monitoring on
it. If you have any questions, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #6
Hi Martin,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Randy Crockett | last post by:
Does anyone know how I can extract the Icon from a Program(like Excel) or a File (*.xls) and use that icon on a runtime generated button I am using .NET 2003 with Framework 1.1 and I am coding in...
1
by: Víctor | last post by:
I'm doing a kind of file explorer with some additional funcionalities. The problem is that I'm not able to draw file extension icons in a ListView. I know how obtain icons, but when I try to draw...
1
by: Jesper | last post by:
Hi, I've done some file type assignment to a program I've made such that e.g. when I double click on files of type .foo it will be openened in my program. However, I'm still missing two...
0
by: Carl Mercier | last post by:
(I'm using C# 2.0) Hi, I have placed an icon file in resources.resx. The problem is that I don't know how to retrieve it. In VB.NET 2.0, My.Resources.MyIcon does the trick, but what's the...
4
by: marco.nl | last post by:
it fails to retrieve newly intruduced data by the user. i tried this ... AfxGetApp()->WriteProfileString("Settings", "email", EDIT1); UpdateData(FALSE); and this.....
5
by: IcingDeath via DotNetMonster.com | last post by:
I am building this SQL Server database app in which i can store files. In order to display files I want to have the app show the associated icon for the extension of the file that is in the...
9
by: Rotzooi | last post by:
Hi, I have a VB.NET Service application that's running fine under the Local System account. But for configuration purposes I don't want to be dependent on modifying the registry manually or...
2
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; How can I set the icon for the DLL file a portla deploy project creates? -- thanks - dave david_at_windward_dot_net http://www.windwardreports.com Cubicle Wars -...
5
by: =?Utf-8?B?TG9hbldvbGY=?= | last post by:
I'm working on a Windows Explorer-like application. What is the best way to find out what application is set to open a particular type of file? Is there a function to do this? Do I have to go...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.