Skip to content

Commit

Permalink
Added remove to recycle bin utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Apr 24, 2019
1 parent e20df39 commit 78126fa
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions KKAPI/KKAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
<Compile Include="Studio\UI\CurrentStateCategory.cs" />
<Compile Include="Studio\UI\CurrentStateCategorySubItemBase.cs" />
<Compile Include="Studio\UI\CurrentStateCategoryToggle.cs" />
<Compile Include="Utilities\RecycleBinUtil.cs" />
<Compile Include="Utilities\OpenFileDialog.cs" />
<Compile Include="Utilities\Utils.cs" />
</ItemGroup>
Expand Down
134 changes: 134 additions & 0 deletions KKAPI/Utilities/RecycleBinUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Runtime.InteropServices;

namespace KKAPI.Utilities
{
/// <summary>
/// Allows to move files to recycle bin instead of completely removing them.
/// https://stackoverflow.com/questions/3282418/send-a-file-to-the-recycle-bin?answertab=votes#tab-top
/// </summary>
public static class RecycleBinUtil
{
/// <summary>
/// Send file to recycle bin
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
/// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
public static bool MoveToRecycleBin(string path, FileOperationFlags flags)
{
try
{
var fs = new SHFILEOPSTRUCT
{
wFunc = FileOperationType.FO_DELETE,
pFrom = path + '\0' + '\0',
fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags
};
SHFileOperation(ref fs);
return true;
}
catch (Exception)
{
return false;
}
}

/// <summary>
/// Send file silently to recycle bin. Surpress dialog, surpress errors, delete if too large.
/// </summary>
/// <param name="path">Location of directory or file to recycle</param>
public static bool MoveToRecycleBin(string path)
{
return MoveToRecycleBin(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT);
}

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

/// <summary>
/// Possible flags for the SHFileOperation method.
/// </summary>
[Flags]
public enum FileOperationFlags : ushort
{
/// <summary>
/// Do not show a dialog during the process
/// </summary>
FOF_SILENT = 0x0004,

/// <summary>
/// Do not ask the user to confirm selection
/// </summary>
FOF_NOCONFIRMATION = 0x0010,

/// <summary>
/// Delete the file to the recycle bin. (Required flag to send a file to the bin
/// </summary>
FOF_ALLOWUNDO = 0x0040,

/// <summary>
/// Do not show the names of the files or folders that are being recycled.
/// </summary>
FOF_SIMPLEPROGRESS = 0x0100,

/// <summary>
/// Surpress errors, if any occur during the process.
/// </summary>
FOF_NOERRORUI = 0x0400,

/// <summary>
/// Warn if files are too big to fit in the recycle bin and will need
/// to be deleted completely.
/// </summary>
FOF_WANTNUKEWARNING = 0x4000
}

/// <summary>
/// File Operation Function Type for SHFileOperation
/// </summary>
private enum FileOperationType : uint
{
/// <summary>
/// Move the objects
/// </summary>
FO_MOVE = 0x0001,

/// <summary>
/// Copy the objects
/// </summary>
FO_COPY = 0x0002,

/// <summary>
/// Delete (or recycle) the objects
/// </summary>
FO_DELETE = 0x0003,

/// <summary>
/// Rename the object(s)
/// </summary>
FO_RENAME = 0x0004
}

/// <summary>
/// SHFILEOPSTRUCT for SHFileOperation from COM
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SHFILEOPSTRUCT
{
public readonly IntPtr hwnd;

[MarshalAs(UnmanagedType.U4)]
public FileOperationType wFunc;

public string pFrom;
public readonly string pTo;
public FileOperationFlags fFlags;

[MarshalAs(UnmanagedType.Bool)]
public readonly bool fAnyOperationsAborted;

public readonly IntPtr hNameMappings;
public readonly string lpszProgressTitle;
}
}
}
1 change: 1 addition & 0 deletions doc/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@
- [`Extensions`](KKAPI.Utilities.md#extensions)
- [`OpenFileDialog`](KKAPI.Utilities.md#openfiledialog)
- [`ReadOnlyDictionary<TKey, TValue>`](KKAPI.Utilities.md#readonlydictionarytkey-tvalue)
- [`RecycleBinUtil`](KKAPI.Utilities.md#recyclebinutil)

16 changes: 16 additions & 0 deletions doc/KKAPI.Utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ Methods
| `Boolean` | TryGetValue(`TKey` key, `TValue&` value) | |


## `RecycleBinUtil`

Allows to move files to recycle bin instead of completely removing them. https://stackoverflow.com/questions/3282418/send-a-file-to-the-recycle-bin?answertab=votes#tab-top
```csharp
public static class KKAPI.Utilities.RecycleBinUtil

```

Static Methods

| Type | Name | Summary |
| --- | --- | --- |
| `Boolean` | MoveToRecycleBin(`String` path, `FileOperationFlags` flags) | Send file to recycle bin |
| `Boolean` | MoveToRecycleBin(`String` path) | Send file to recycle bin |


0 comments on commit 78126fa

Please sign in to comment.