-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
301 lines (281 loc) · 11.7 KB
/
Utils.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.VisualBasic.FileIO;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
public static class FileSizeHelper
{
static readonly string[] SizeSuffixes = { "B ", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
const long byteConversion = 1000;
public static string GetHumanReadableFileSize(long value)
{
if (value == 0) { return "0"; }
int mag = (int)Math.Log(value, byteConversion);
int adjustedSize = (int)(value / Math.Pow(1000, mag));
return string.Format("{0:d} {1}", adjustedSize, SizeSuffixes[mag]);
}
}
public class FileSystemHelper
{
public static void OperateFileSystemItem(
string sourceName,
string destinationName,
DragDropEffects operationEffect
)
{
UIOption showUI = UIOption.AllDialogs;
UICancelOption onUserCancel = UICancelOption.DoNothing;
if (operationEffect.HasFlag(DragDropEffects.Move))
{
if (sourceName == destinationName) return;
if (File.Exists(sourceName))
FileSystem.MoveFile(sourceName, destinationName, showUI, onUserCancel);
else if (Directory.Exists(sourceName))
FileSystem.MoveDirectory(sourceName, destinationName, showUI, onUserCancel);
}
else
{
if (sourceName == destinationName) destinationName = Path.Combine(
Path.GetDirectoryName(sourceName),
Path.GetFileNameWithoutExtension(sourceName) + " - Copy" +
Path.GetExtension(sourceName)
);
if (File.Exists(sourceName))
FileSystem.CopyFile(sourceName, destinationName, showUI, onUserCancel);
else if (Directory.Exists(sourceName))
FileSystem.CopyDirectory(sourceName, destinationName, showUI, onUserCancel);
}
}
public static void DeleteFileSystemItem(
string itemName,
RecycleOption recycle
)
{
UIOption showUI = UIOption.AllDialogs;
UICancelOption onUserCancel = UICancelOption.DoNothing;
if (File.Exists(itemName))
FileSystem.DeleteFile(itemName, showUI, recycle, onUserCancel);
else if (Directory.Exists(itemName))
FileSystem.DeleteDirectory(itemName, showUI, recycle, onUserCancel);
}
}
public class ShellInfoHelper
{
public const uint SHGFI_DISPLAYNAME = 0x200;
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0;
public const uint SHGFI_SMALLICON = 0x1;
public const uint SHGFI_USEFILEATTRIBUTES = 0x10;
public const uint SHGFI_SYSICONINDEX = 0x4000;
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHFILEINFOW
{
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;
};
public static string GetDownloadFolderPath()
{
return SHGetKnownFolderPath(new Guid("374DE290-123F-4565-9164-39C4925E467B"), 0, IntPtr.Zero);
}
[DllImport("shell32", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = false)]
private static extern string SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
uint dwFlags,
IntPtr hToken);
[DllImport("shell32")]
public static extern IntPtr SHGetFileInfoW([MarshalAs(UnmanagedType.LPWStr)] string pszPath,
uint dwFileAttributes,
out SHFILEINFOW psfi,
uint cbSizeFileInfo,
uint uFlags);
[DllImport("user32")]
public static extern int DestroyIcon(IntPtr hIcon);
public static Image GetIconFromPath(string pszPath, bool simallIcon = true)
{
SHFILEINFOW info = new SHFILEINFOW();
uint flag = SHGFI_ICON;
flag |= simallIcon ? SHGFI_SMALLICON : SHGFI_LARGEICON;
SHGetFileInfoW(pszPath, 0, out info, (uint)Marshal.SizeOf(info), flag);
if (info.hIcon == (IntPtr)0)
{
DestroyIcon(info.hIcon);
return new Bitmap(32, 32);
}
else
{
Icon icon = Icon.FromHandle(info.hIcon);
Image img = icon.ToBitmap();
icon.Dispose();
DestroyIcon(info.hIcon);
return img;
}
}
public static string GetDisplayNameFromPath(string pszPath)
{
SHFILEINFOW info = new SHFILEINFOW();
SHGetFileInfoW(pszPath, 0, out info, (uint)Marshal.SizeOf(info), SHGFI_DISPLAYNAME);
return info.szDisplayName;
}
public static string GetExactPathName(string pathName)
{
var di = new DirectoryInfo(pathName);
if (di.Parent != null)
{
return Path.Combine(
GetExactPathName(di.Parent.FullName),
di.Parent.GetFileSystemInfos(di.Name)[0].Name);
}
else
{
if (di.FullName.StartsWith("\\\\") || di.FullName.StartsWith("//"))
return Path.GetPathRoot(di.FullName);
else
return di.Name.ToUpper();
}
}
}
public class ShellApplicationHelper
{
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
internal interface IServiceProvider
{
void QueryService([MarshalAs(UnmanagedType.LPStruct)] Guid guidService, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObject);
}
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214e2-0000-0000-c000-000000000046")]
internal interface IShellBrowser
{
IntPtr GetWindow();
}
public static IntPtr GetTabHwnd(object window)
{
IServiceProvider sp = window as IServiceProvider;
object sb;
sp.QueryService(Guid.Parse("000214e2-0000-0000-c000-000000000046"), typeof(IShellBrowser).GUID, out sb);
IShellBrowser shellBrowser = (IShellBrowser)sb;
return (IntPtr)shellBrowser.GetWindow();
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32")]
private static extern void GetClassName(IntPtr hWnd, StringBuilder s, int nMaxCount);
public static string GetClassName(IntPtr hWnd)
{
StringBuilder sb = new StringBuilder(256);
GetClassName(hWnd, sb, 256);
return sb.ToString();
}
[DllImport("user32")]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
private static bool EnumTabWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (GetClassName(handle) == "ShellTabWindowClass")
{
list.Add(handle);
}
return true;
}
public static IntPtr GetActiveTabHwnd(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumTabWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result[0];
}
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
public static void NavigateExplorerTo(string path)
{
if (!Directory.Exists(path)) return;
IntPtr TopExplorerHwnd = FindWindow("CabinetWClass", null);
if (TopExplorerHwnd == IntPtr.Zero)
{
Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
return;
}
IntPtr activateTabHwnd = GetActiveTabHwnd(TopExplorerHwnd);
object shellApplication = System.Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
object windows = shellApplication.GetType().InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, null);
int windowCounts = (int)windows.GetType().InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);
for (int i = 0; i < windowCounts; i++)
{
object window = windows.GetType().InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
IntPtr hwnd = (IntPtr)(long)window.GetType().InvokeMember("HWND", System.Reflection.BindingFlags.GetProperty, null, window, null);
IntPtr tabHwnd = GetTabHwnd(window);
if (hwnd != TopExplorerHwnd || tabHwnd != activateTabHwnd)
{
Marshal.ReleaseComObject(window);
continue;
}
else
{
if (path == "")
{
window.GetType().InvokeMember("Refresh", System.Reflection.BindingFlags.InvokeMethod, null, window, null);
}
else
{
window.GetType().InvokeMember("Navigate", System.Reflection.BindingFlags.InvokeMethod, null, window, new object[] { new Uri(path).ToString() });
}
Marshal.ReleaseComObject(window);
break;
}
}
Marshal.ReleaseComObject(windows);
Marshal.ReleaseComObject(shellApplication);
}
public static List<string> GetExplorerPaths()
{
object shellApplication = System.Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
object windows = shellApplication.GetType().InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, null);
int windowCounts = (int)windows.GetType().InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);
List<string> output = new List<string>();
for (int i = 0; i < windowCounts; i++)
{
object window = windows.GetType().InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
string locationURL = (string)window.GetType().InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, window, null);
if (locationURL != "")
{
output.Add(new Uri(locationURL).LocalPath);
}
Marshal.ReleaseComObject(window);
};
Marshal.ReleaseComObject(windows);
Marshal.ReleaseComObject(shellApplication);
return output;
}
}
namespace DropTransfer
{
public class DpiFactor
{
private float scaling;
public DpiFactor(float scale)
{
scaling = scale;
}
public static int operator *(int pt, DpiFactor factor)
{
return (int)(pt * factor.scaling);
}
}
}