-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileInfo.cs
130 lines (122 loc) · 5.42 KB
/
FileInfo.cs
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
using Shell32;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using static Micro.Menu.Core;
namespace Micro.Menu {
public class FileInfo {
public const string internalReq = "InternetShortcut";
public readonly bool nonExistant;
public string description {
get {
if (!string.IsNullOrWhiteSpace(_desc))
return _desc;
else {
var p = isShortcut && extension == ".lnk" ?
destination :
extension == ".exe" ?
path : null;
if (p == null || !File.Exists(p))
return null;
var fvi = FileVersionInfo.GetVersionInfo(p);
return string.IsNullOrWhiteSpace(fvi.FileDescription) ?
string.IsNullOrWhiteSpace(fvi.ProductName) ?
null :
fvi.ProductName :
fvi.FileDescription;
}
}
}
public string finalPath
=> isShortcut ? destination : path;
public string parentDir
=> Directory.GetParent(finalPath).FullName;
public string finalWorkDir
=> extension == ".url" ? "" :
string.IsNullOrWhiteSpace(workingDir) ? parentDir : workingDir;
public bool hasIcon
=> icon != null;
public string name, path, extension, destination, destExt, arguments, workingDir, iconPath;
public int iconIndex;
public bool isShortcut, isDirectory;
public Image icon;
string _desc;
public FileInfo(string filePath, string args = null, string workDir = null, string name = null, Image icon = null) {
path = filePath;
extension = Path.GetExtension(filePath).ToLower();
this.name = name ?? Path.GetFileNameWithoutExtension(filePath).Replace("&", "&&");
nonExistant = !File.Exists(filePath) && !(isDirectory = Directory.Exists(filePath));
if (!nonExistant) {
arguments = args;
workingDir = workDir;
resolveLink();
this.icon = icon ?? getImage();
}
}
void resolveLink() {
if (!isDirectory) {
isShortcut = (extension == ".lnk" || extension == ".url");
switch (extension) {
case ".lnk":
string lnkPath = path.Substring(0, path.LastIndexOf(@"\"));
string lnkName = path.Substring(path.LastIndexOf(@"\") + 1);
if (!lnkName.EndsWith(".lnk"))
lnkName += ".lnk";
Folder lnkFolder = shell.NameSpace(lnkPath);
FolderItem lnkItem = lnkFolder.Items().Item(lnkName);
if (lnkItem == null || !lnkItem.IsLink)
return;
try {
ShellLinkObject lnk = (ShellLinkObject)lnkItem.GetLink;
destination = HandleWoW(lnk.Path);
destExt = Path.GetExtension(destination).ToLower();
arguments = arguments ?? lnk.Arguments;
workingDir = workingDir ?? lnk.WorkingDirectory;
try { _desc = lnk.Description; } catch { }
iconIndex = lnk.GetIconLocation(out iconPath);
} catch {
isShortcut = false;
}
break;
case ".url":
destination = IniReadValue(path, internalReq, "URL");
iconPath = IniReadValue(path, internalReq, "IconFile");
int.TryParse(IniReadValue(path, internalReq, "IconIndex"), out iconIndex);
break;
}
}
}
Image getImage() {
for (int way = 1; way <= 5; way++) {
switch (way) {
case 1 when !string.IsNullOrWhiteSpace(iconPath):
try { return ExtractImage(iconPath, iconIndex); } catch { }
break;
case 2 when (extension == ".url" || extension == ".lnk") && destExt == ".exe":
try { return ExtractImage(destination); } catch {
if (extension != ".url")
return ExtractImage(@"%systemroot%\system32\imageres.dll", 11);
}
break;
case 4:
if (isDirectory(path))
return GetIcon(path);
else
try { return ExtractImage(path); } catch { }
break;
case 5:
return GetIcon(path);
}
}
return null;
}
public static string HandleWoW(string path) {
string np;
if (isWoW && path.StartsWith(programfiles) && !File.Exists(path))
if (File.Exists(np = path.Replace(programfiles, programw6432)))
return np;
return path;
}
}
}