| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace Azylee.Core.ProcessUtils
- {
- public class ProcessInfoTool
- {
- #region 程序集信息
- struct SHFILEINFO
- {
- public IntPtr hIcon;
- public int iIcon;
- public uint dwAttributes;
- [MarshalAs(UnmanagedType.LPStr)]
- public string szDisplayName;
- [MarshalAs(UnmanagedType.LPStr)]
- public string szTypeName;
- public SHFILEINFO(bool b)
- {
- this.hIcon = IntPtr.Zero;
- this.iIcon = 0;
- this.dwAttributes = 0u;
- this.szDisplayName = "";
- this.szTypeName = "";
- }
- }
- enum SHGFI
- {
- SmallIcon = 1,
- LargeIcon = 0,
- Icon = 256,
- DisplayName = 512,
- Typename = 1024,
- SysIconIndex = 16384,
- UseFileAttributes = 16
- }
- #endregion
- [DllImport("Shell32.dll")]
- static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags);
- static Icon GetIcon(string file, bool small)
- {
- try
- {
- SHFILEINFO sHFILEINFO = new SHFILEINFO(true);
- int cbfileInfo = Marshal.SizeOf(sHFILEINFO);
- SHGFI uFlags;
- if (small)
- {
- uFlags = (SHGFI)273;
- }
- else
- {
- uFlags = (SHGFI)272;
- }
- SHGetFileInfo(file, 256u, out sHFILEINFO, (uint)cbfileInfo, uFlags);
- return Icon.FromHandle(sHFILEINFO.hIcon);
- }
- catch { }
- return null;
- }
- public static Icon GetIcon(Process p, bool small)
- {
- try
- {
- string fileName = p.MainModule.FileName;
- return GetIcon(fileName, small);
- }
- catch { }
- return null;
- }
- [Obsolete]
- public static Icon GetIcon(int pid, bool small)
- {
- Process p = Process.GetProcessById(pid);
- Icon ic = GetIcon(p, small);
- p.Close();
- return ic;
- }
- [Obsolete]
- public static string GetNameById(int pid)
- {
- string result = "";
- try
- {
- Process processById = Process.GetProcessById(pid);
- result = processById.ProcessName;
- processById.Close();
- }
- catch (Exception ex) { }
- return result.Trim();
- }
- }
- }
|