473,663 Members | 2,719 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 7567
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
1328
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
15056
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
1488
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
1672
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
3439
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
3160
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
6008
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
1339
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
6672
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
8435
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
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7368
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...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
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
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
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

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.