Total members 11894 |It is currently Sat Nov 23, 2024 3:45 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






 Project Name:   Get Registered File Types and Their Associated Icons in C#
 Programmer:   kidvn
 Type:   Application
 Technology:  C#
 IDE:   NONE
 Description:   How can we get the icon of a file if it does not exist? If we only know its name or even its extension, how do we get the icon associated with it? This is a simple project of mine to get the registered file types and the associated icons by reading from Windows Registry.

code sample
csharp code
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FileTypeAndIcon
{
public partial class IconForm : Form
{
#region PROPERTIES

/// <summary>
/// Used for containing file types and thier icons information.
/// </summary>
private Hashtable icons;

/// <summary>
/// USed for getting registered file types and their icons information.
/// </summary>
private RegisteredFileType fileType;

#endregion

#region CONSTRUCTORS

public IconForm()
{
InitializeComponent();
this.fileType = new RegisteredFileType();
}

#endregion

#region GUI EVENTS

private void IconForm_Load(object sender, EventArgs e)
{
try
{
//Gets file type and icon info.
this.icons = this.fileType.GetFileTypeAndIcon();

//Loads file types to ListBox.
foreach (object objString in this.icons.Keys)
{
this.lbxFileType.Items.Add(objString);
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}

private void btnSearch_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtSearch.Text.Trim()))
return;
string searchName = "";
if (!this.txtSearch.Text.Contains("."))
searchName = "." + this.txtSearch.Text; //Add a dot if the search text does not have it.
else
searchName = this.txtSearch.Text;

//Searches in the collections of file types and icons.
object objName = this.icons[searchName];
if (objName != null)
{
this.lbxFileType.SelectedItem = searchName;
}
}

private void lbxFileType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (this.lbxFileType.Items.Count <= 0 || this.lbxFileType.SelectedItem == null)
return;
string fileType = this.lbxFileType.SelectedItem.ToString();
this.showIcon(fileType);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}

private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtSearch.Text.Trim()))
{
this.btnSearch.Enabled = false;
}
else
{
this.btnSearch.Enabled = true;
}
}

/// <summary>
/// Shows the icon associates with a specific file type.
/// </summary>
/// <param name="fileType">The type of file (or file extension).</param>
private void showIcon(string fileType)
{
try
{
string fileAndParam = (this.icons[fileType]).ToString();
if (String.IsNullOrEmpty(fileAndParam))
return;

//Use to store the file contains icon.
string fileName = "";

//The index of the icon in the file.
int iconIndex = 0;
string iconIndexString = "";

int index = fileAndParam.IndexOf(",");
//if fileAndParam is some thing likes that: "C:\\Program Files\\NetMeeting\\conf.exe,1".
if (index > 0)
{
fileName = fileAndParam.Substring(0, index);
iconIndexString = fileAndParam.Substring(index + 1);
}
else
fileName = fileAndParam;

if (!string.IsNullOrEmpty(iconIndexString))
{
//Get the index of icon.
iconIndex = int.Parse(iconIndexString);
if (iconIndex < 0)
iconIndex = 0; //To avoid the invalid index.
}

//Gets the handle of the icon.
IntPtr lIcon = RegisteredFileType.ExtractIcon(this.Handle.ToInt32(), fileName, iconIndex);

//The handle cannot be zero.
if (lIcon != IntPtr.Zero)
{
//Gets the real icon.
Icon icon = Icon.FromHandle(lIcon);

//Draw the icon to the picture box.
this.pbIconView.Image = icon.ToBitmap();
}
else //if the icon is invalid, show an error image.
this.pbIconView.Image = this.pbIconView.ErrorImage;
}
catch (Exception exc)
{
throw exc;
}
}

#endregion
}
}

Attachment:
preview.png
preview.png [ 6.44 KiB | Viewed 6992 times ]





Attachments:
FileTypeAndIcon_src.zip [18.32 KiB]
Downloaded 1814 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Get Registered File Types and Their Associated Icons in C#
 Encrypt/Decrypt a file from source file to target file.     -  
 Types of Registers     -  
 Types of Pointers in C++     -  
 java data types     -  
 assembler directives types     -  
 temperature transformer between different types     -  
 pointers to derived types     -  
 What are the types of jdbc drivers.?     -  
 lesson9: XSD Complex Types Indicators     -  
 MYSQL TEXT field types lengths     -  



Topic Tags

C# Projects







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com