473,857 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7574
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 ExtractAssociat edIcon
[DllImport("shel l32.dll")]
static extern IntPtr ExtractAssociat edIcon(IntPtr hinst, string file,
ref int index);

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

RegWin32.Regist ryKey key =
RegWin32.Regist ry.ClassesRoot. OpenSubKey(ext) ;

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

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

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

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

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(i ndex+1) );
}
}//index
else
name = val;

System.Reflecti on.Module [] mod =
System.Reflecti on.Assembly.Get CallingAssembly ().GetModules() ;
IntPtr ptr =
ExtractAssociat edIcon(Marshal. GetHINSTANCE(mo d[0]), name, ref icoIndex);

if(ptr != IntPtr.Zero)
{
return System.Drawing. Icon.FromHandle (ptr);
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show (ex.Message+"\n "+ex.InnerExcep tion);
}
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 ExtractAssociat edIcon does not.

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

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

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

#region ExtractAssociat edIcon
[DllImport("shel l32.dll")]
static extern IntPtr ExtractAssociat edIcon(IntPtr hinst, string file, ref
int index);

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

RegWin32.Regist ryKey key = RegWin32.Regist ry.ClassesRoot. OpenSubKey(ext) ;

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

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

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

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

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(i ndex+1) );
}
}//index
else
name = val;

System.Reflecti on.Module [] mod =
System.Reflecti on.Assembly.Get CallingAssembly ().GetModules() ;
IntPtr ptr = ExtractAssociat edIcon(Marshal. GetHINSTANCE(mo d[0]), name, ref
icoIndex);

if(ptr != IntPtr.Zero)
{
return System.Drawing. Icon.FromHandle (ptr);
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show (ex.Message+"\n "+ex.InnerExcep tion);
}
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
1340
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 C# The platforms are Windows 98 and up I have an app that will call up a file and luanch it's Windows Associated program when I click on a button for that file. I would like the button to have the same Icon as the file is displayed in Explorer ...
1
15112
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 file icon, this isn't showed. I tried to do by two ways: 1- By creating the ListViewItem and adding it to ListView, then I get rectangle of icon element and paint a new icon in rectangle coordenates. I'm able to view icons while they are...
1
1499
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 things: 1) When I right click on the .foo file (in MS Windows) and selects properties, my application is only shown with its icon under the 'opens with:' label and not with the name of the program like it is with e.g.
0
1679
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 C# equivalent? Unfortunately, what happens under the hood with the My namespace is not well documented. Reflector on Microsoft.VisualBasic.dll
4
3446
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.. CDHtmlDialog::GetElementText(_T("email")); if(CDHtmlDialog::GetElementText(_T("email"))) {
5
3177
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 database. Therefore the file doesnt really exist on the user's hard drive. All the file extracting techniques require that I have the actual file on the drive in order to get an icon for it. There is a workaround for this: i could create dummy files with...
9
6029
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 using an seperate application (like I do now). I want to implement a system tray icon when someone logs on to the console. I've tried several samples but I can't get the icon to the system tray. I know there are some issues because the logged on...
2
1348
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 - http://www.windwardreports.com/film.htm
5
6690
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 through the registry and check ClassesRoot? Also, once I have the application, I want to be able to get the icon for that application. How would I get this?
0
9923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11082
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9549
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5967
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4190
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.