-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFileSystemInfoExtensions.cs
More file actions
92 lines (85 loc) · 2.65 KB
/
Copy pathFileSystemInfoExtensions.cs
File metadata and controls
92 lines (85 loc) · 2.65 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
using Microsoft.VisualBasic.FileIO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace BytecodeApi.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="FileSystemInfo" /> objects.
/// </summary>
public static class FileSystemInfoExtensions
{
extension(FileSystemInfo fileSystemInfo)
{
/// <summary>
/// Shows the properties dialog for this file or directory. The dialog closes, when this process exits.
/// </summary>
[SupportedOSPlatform("windows")]
public void ShowPropertiesDialog()
{
Check.ArgumentNull(fileSystemInfo);
Check.FileOrDirectoryNotFound(fileSystemInfo.FullName);
Native.ShellExecuteInfo info = new()
{
StructSize = Marshal.SizeOf<Native.ShellExecuteInfo>(),
Verb = "properties",
FileName = fileSystemInfo.FullName,
Show = 5,
Mask = 0x50c
};
if (!Native.ShellExecuteEx(ref info))
{
throw Throw.Win32("Could not open properties dialog.");
}
}
/// <summary>
/// Sends this file or directory to recycle bin.
/// </summary>
[SupportedOSPlatform("windows")]
public void SendToRecycleBin()
{
Check.ArgumentNull(fileSystemInfo);
Check.FileOrDirectoryNotFound(fileSystemInfo.FullName);
if (fileSystemInfo is DirectoryInfo && fileSystemInfo.Exists)
{
FileSystem.DeleteDirectory(fileSystemInfo.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
}
else if (fileSystemInfo is FileInfo && fileSystemInfo.Exists)
{
FileSystem.DeleteFile(fileSystemInfo.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException);
}
else
{
throw Throw.UnsupportedType(nameof(fileSystemInfo));
}
}
}
}
[SupportedOSPlatform("windows")]
file static class Native
{
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern bool ShellExecuteEx(ref ShellExecuteInfo execInfo);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ShellExecuteInfo
{
public int StructSize;
public uint Mask;
public nint Handle;
[MarshalAs(UnmanagedType.LPTStr)]
public string Verb;
[MarshalAs(UnmanagedType.LPTStr)]
public string FileName;
[MarshalAs(UnmanagedType.LPTStr)]
public string Parameters;
[MarshalAs(UnmanagedType.LPTStr)]
public string Directory;
public int Show;
public nint Instance;
public nint ItemIdList;
[MarshalAs(UnmanagedType.LPTStr)]
public string ClassName;
public nint ClassKey;
public uint HotKey;
public nint Icon;
public nint Process;
}
}