博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#获取软件图标
阅读量:6863 次
发布时间:2019-06-26

本文共 1799 字,大约阅读时间需要 5 分钟。

 
using
System;
using
System.Collections.Generic;
using
System.Drawing;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.IO;
namespace
ShowIcon
{
[StructLayout( LayoutKind.Sequential)]
public
struct
FileInfomation
{
public
IntPtr hIcon;
public
int
iIcon;
public
int
dwAttributes;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst
=
260
)]
public
string
szDisplayName;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst
=
80
)]
public
string
szTypeName;
}
///
<summary>
///
Description of MainForm.
///
</summary>
public
partial
class
MainForm
{
[STAThread]
public
static
void
Main(
string
[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false
);
Application.Run(
new
MainForm());
}
[DllImport(
"
shell32.dll
"
, EntryPoint
=
"
SHGetFileInfo
"
)]
public
static
extern
int
GetFileInfo(
string
pszPath,
int
dwFileAttributes,
ref
FileInfomation psfi,
int
cbFileInfo,
int
uFlags);
public
MainForm()
{
//
//
The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
//
TODO: Add constructor code after the InitializeComponent() call.
//
}
public
static
Icon GetSmallIcon(
string
path)
{
FileInfomation _info
=
new
FileInfomation();
GetFileInfo(path,
0
,
ref
_info, Marshal.SizeOf(_info), (
int
)(
0x000000100
|
0x000000001
));
try
{
return
Icon.FromHandle(_info.hIcon);
}
catch
{
return
null
;
}
}
void
Button1Click(
object
sender, System.EventArgs e)
{
this
.openFileDialog1.ShowDialog();
}
void
OpenFileDialog1FileOk(
object
sender, System.ComponentModel.CancelEventArgs e)
{
this
.textBox1.Text
=
this
.openFileDialog1.FileName;
this
.pictureBox1.Image
=
GetSmallIcon(
this
.openFileDialog1.FileName).ToBitmap();
}
}
}

转载于:https://www.cnblogs.com/longle/archive/2011/06/04/2072908.html

你可能感兴趣的文章
Angular-ui/bootstarp modal 主控制器与模态框控制器传值
查看>>
easyui-datagrid 编辑模式详解
查看>>
[iOS]UIScrollView嵌套内容在左右拨动的时候自动被顶上问题
查看>>
Memcached安装和使用
查看>>
读取TXT文件,转成Table
查看>>
一、快速排序
查看>>
词法分析
查看>>
Linux 安装python3.x步骤
查看>>
安卓多线程的实现
查看>>
【现在还没补的比赛及题解】
查看>>
C#截取字符串按字节截取SubString
查看>>
MAVLink v1.0详解——结构
查看>>
Office 365离线安装
查看>>
服务器负载暴涨以后...
查看>>
【物联网智能网关-15】WAV播放器(WinForm+WavPlay库实例)
查看>>
实战:将静态路由发布到动态路由
查看>>
Linux桌面新彩虹-Fedora 14 炫酷应用新体验
查看>>
灵活管理Hadoop各发行版的运维利器 - vSphere Big Data Extensions
查看>>
Data Protection Manager 2010 系列之安装部署
查看>>
So what, So TM what?
查看>>