Skip to content

Commit 4822494

Browse files
committed
Add FileMan
1 parent 86c6df7 commit 4822494

File tree

10 files changed

+60
-3
lines changed

10 files changed

+60
-3
lines changed

.vs/QuickLibrary/v16/.suo

6.5 KB
Binary file not shown.

QuickLibrary/FileMan.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace QuickLibrary
5+
{
6+
public static class FileMan
7+
{
8+
public static string GetFileSizeStr(string filePath)
9+
{
10+
return GetFileSizeStr(new FileInfo(filePath).Length);
11+
}
12+
13+
public static string GetFileSizeStr(double fileLength)
14+
{
15+
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
16+
int order = 0;
17+
while (fileLength >= 1024 && order < sizes.Length - 1)
18+
{
19+
order++;
20+
fileLength = fileLength / 1024;
21+
}
22+
return String.Format("{0:0.##} {1}", fileLength, sizes[order]);
23+
}
24+
25+
public static void MoveFileOrFolderToRecycleBin(string path)
26+
{
27+
NativeMan.SHFILEOPSTRUCT fileop = new NativeMan.SHFILEOPSTRUCT();
28+
fileop.wFunc = NativeMan.FO_DELETE;
29+
fileop.pFrom = path + '\0' + '\0';
30+
fileop.fFlags = NativeMan.FOF_ALLOWUNDO | NativeMan.FOF_NOCONFIRMATION;
31+
NativeMan.SHFileOperation(ref fileop);
32+
}
33+
}
34+
}

QuickLibrary/NativeMan.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static class NativeMan
3030
public const int SW_SHOW = 5;
3131
public const int SEE_MASK_INVOKEIDLIST = 12;
3232
public const int WS_EX_TRANSPARENT = 0x20;
33+
public const int FO_DELETE = 0x0003;
34+
public const int FOF_ALLOWUNDO = 0x0040;
35+
public const int FOF_NOCONFIRMATION = 0x0010;
36+
3337

3438
// ENUMS
3539

@@ -77,7 +81,7 @@ public enum ScrollBarDirection
7781

7882
// STRUCTURES
7983

80-
[StructLayout(LayoutKind.Sequential)]
84+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
8185
public struct MARGINS
8286
{
8387
public int Left;
@@ -86,7 +90,7 @@ public struct MARGINS
8690
public int Bottom;
8791
}
8892

89-
[StructLayout(LayoutKind.Sequential)]
93+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
9094
public struct SHELLEXECUTEINFO
9195
{
9296
public int cbSize;
@@ -111,6 +115,21 @@ public struct SHELLEXECUTEINFO
111115
public IntPtr hProcess;
112116
}
113117

118+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
119+
public struct SHFILEOPSTRUCT
120+
{
121+
public IntPtr hwnd;
122+
[MarshalAs(UnmanagedType.U4)]
123+
public int wFunc;
124+
public string pFrom;
125+
public string pTo;
126+
public short fFlags;
127+
[MarshalAs(UnmanagedType.Bool)]
128+
public bool fAnyOperationsAborted;
129+
public IntPtr hNameMappings;
130+
public string lpszProgressTitle;
131+
}
132+
114133
// KERNEL32 METHODS
115134

116135
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
@@ -175,6 +194,9 @@ public struct SHELLEXECUTEINFO
175194
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
176195
public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
177196

197+
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
198+
public static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
199+
178200
// CUSTOM METHODS
179201

180202
public static string GetMessageBoxText(DialogBoxCommandID messageId)

QuickLibrary/QuickLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
</Compile>
6565
<Compile Include="DropShadow.cs" />
6666
<Compile Include="FileAssociation.cs" />
67+
<Compile Include="FileMan.cs" />
6768
<Compile Include="LangMan.cs" />
6869
<Compile Include="NativeMan.cs" />
6970
<Compile Include="PluginInfo.cs" />
1 KB
Binary file not shown.
2 KB
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6493d2b7a2f809cde8131b2a0956d3382cf32d79
1+
6e4b1d0923300b681f587dbf4e8a9c2df57b659d
1 KB
Binary file not shown.
2 KB
Binary file not shown.

0 commit comments

Comments
 (0)