-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgramItem.cs
More file actions
161 lines (144 loc) · 5.87 KB
/
ProgramItem.cs
File metadata and controls
161 lines (144 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace WindowsSmartTaskbar
{
public class ProgramItem
{
public string Name { get; set; } = string.Empty;
public string FilePath { get; set; } = string.Empty;
public string? Arguments { get; set; }
public string Category { get; set; } = "All programs";
public Icon? Icon { get; set; }
public DateTime AddedDate { get; set; } = DateTime.Now;
public ProgramItem() { }
public ProgramItem(string name, string filePath, string? arguments = null)
{
Name = name;
FilePath = filePath;
Arguments = arguments;
LoadIcon();
}
private void LoadIcon()
{
try
{
string path = FilePath;
if (Path.GetExtension(FilePath).ToLower() == ".lnk")
{
string target = GetShortcutTarget(FilePath);
if (File.Exists(target)) path = target;
}
if (File.Exists(path))
{
SHFILEINFO shfi = new SHFILEINFO();
IntPtr hIcon = SHGetFileInfo(path, 0, ref shfi, (uint)Marshal.SizeOf(shfi), SHGFI_ICON | SHGFI_LARGEICON);
if (hIcon != IntPtr.Zero)
{
Icon = (Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
DestroyIcon(shfi.hIcon);
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"[{nameof(ProgramItem)}::{nameof(LoadIcon)}] {ex}");
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DestroyIcon(IntPtr hIcon);
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0;
private string GetShortcutTarget(string shortcutPath)
{
try
{
string? target = ReadShortcutTarget(shortcutPath);
return string.IsNullOrWhiteSpace(target) ? shortcutPath : target;
}
catch (Exception ex)
{
Debug.WriteLine($"[{nameof(ProgramItem)}::{nameof(GetShortcutTarget)}] {ex}");
return shortcutPath;
}
}
private string? ReadShortcutTarget(string shortcutPath)
{
Type? shellType = Type.GetTypeFromProgID("WScript.Shell");
if (shellType == null) return null;
object? shell = null;
object? shortcut = null;
try
{
shell = Activator.CreateInstance(shellType);
if (shell == null) return null;
shortcut = shellType.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[] { shortcutPath });
if (shortcut == null) return null;
string? targetPath = shortcut.GetType().InvokeMember("TargetPath", BindingFlags.GetProperty, null, shortcut, null) as string;
return string.IsNullOrWhiteSpace(targetPath) ? null : targetPath;
}
catch (Exception ex)
{
Debug.WriteLine($"[{nameof(ProgramItem)}::{nameof(ReadShortcutTarget)}] {ex}");
return null;
}
finally
{
if (shortcut != null && Marshal.IsComObject(shortcut)) Marshal.FinalReleaseComObject(shortcut);
if (shell != null && Marshal.IsComObject(shell)) Marshal.FinalReleaseComObject(shell);
}
}
public void Start()
{
try
{
string targetPath = FilePath;
string targetArgs = Arguments ?? string.Empty;
if (Path.GetExtension(FilePath).ToLower() == ".lnk")
{
try
{
var startInfo = new ProcessStartInfo { FileName = FilePath, UseShellExecute = true };
Process.Start(startInfo);
return;
}
catch (Exception ex)
{
Debug.WriteLine($"[{nameof(ProgramItem)}::{nameof(Start)}] Failed launching shortcut directly: {ex}");
targetPath = GetShortcutTarget(FilePath);
}
}
if (!string.IsNullOrEmpty(targetPath))
{
var startInfo = new ProcessStartInfo { FileName = targetPath, Arguments = targetArgs, UseShellExecute = true };
Process.Start(startInfo);
}
else
{
MessageBox.Show($"Kunde inte hitta målfilen: {targetPath}", "Fel", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Kunde inte starta programmet: {ex.Message}\nSökväg: {FilePath}", "Fel", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}