Skip to content

Commit

Permalink
Fixed files not being deleted if they contained Unicode characters
Browse files Browse the repository at this point in the history
  • Loading branch information
maforget committed May 26, 2024
1 parent 7122094 commit a875b6c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
1 change: 1 addition & 0 deletions ComicRack/Changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Community Edition Build 0.9.180:
* BUGFIX: Fixed missing entry (MainCharacterOrTeam, Review & CommunityRating) not being saved in ComicInfo.xml.
* BUGFIX: Made getting a list of book from a folder a lot faster when there was incompatible files. The slowdown was related to attempting to read the files as a WebComic. Previously the program would attempt to use all it's known provider on each and every file until it found a working one, regardless of the type. Now Webcomics will NEED to be a CBW.
* BUGFIX: Enabling fullscreen didn't minimize the gui when reader was in it's own window, with option to do it at the same time enabled.
* BUGFIX: Fixed files not being deleted if they contained Unicode characters.

^^^^^^^^^^ Community Edition ^^^^^^^^^^

Expand Down
68 changes: 29 additions & 39 deletions cYo.Common/Win32/ShellFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,26 @@ public static class ShellFile
{
private static class UnsafeNativeMethods
{
public struct SHFILEOPSTRUCT
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;

public int wFunc;

public string pFrom;

public string pTo;

public short fFlags;

public int fAnyOperationsAborted;

public bool fAnyOperationsAborted;
public IntPtr hNameMappings;

public string lpszProgressTitle;
}

public const int FO_DELETE = 3;

public const short FOF_SILENT = 4;
public const short FOF_ALLOWUNDO = 0x40;
public const short FOF_NOCONFIRMATION = 0x10;
public const short FOF_NOERRORUI = 0x400;

public const short FOF_ALLOWUNDO = 64;

public const short FOF_NOCONFIRMATION = 16;

public const short FOF_NOERRORUI = 1024;

[DllImport("shell32.dll")]
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);
}

Expand All @@ -59,29 +49,29 @@ public static void DeleteFile(IWin32Window window, ShellFileDeleteOptions option
}
}
if (stringBuilder.Length != 0)
{
stringBuilder.Append('\0');
UnsafeNativeMethods.SHFILEOPSTRUCT lpFileOp = default(UnsafeNativeMethods.SHFILEOPSTRUCT);
lpFileOp.hwnd = window?.Handle ?? IntPtr.Zero;
lpFileOp.wFunc = UnsafeNativeMethods.FO_DELETE;
lpFileOp.fFlags = UnsafeNativeMethods.FOF_SILENT | UnsafeNativeMethods.FOF_NOERRORUI;
if ((options & ShellFileDeleteOptions.NoRecycleBin) == 0)
{
lpFileOp.fFlags |= UnsafeNativeMethods.FOF_ALLOWUNDO;
}
if ((options & ShellFileDeleteOptions.Confirmation) == 0)
{
lpFileOp.fFlags |= UnsafeNativeMethods.FOF_NOCONFIRMATION;
}
lpFileOp.pFrom = stringBuilder.ToString();
lpFileOp.fAnyOperationsAborted = 0;
lpFileOp.hNameMappings = IntPtr.Zero;
if (UnsafeNativeMethods.SHFileOperation(ref lpFileOp) != 0)
{
{
stringBuilder.Append('\0');
UnsafeNativeMethods.SHFILEOPSTRUCT lpFileOp = default(UnsafeNativeMethods.SHFILEOPSTRUCT);
lpFileOp.hwnd = window?.Handle ?? IntPtr.Zero;
lpFileOp.wFunc = UnsafeNativeMethods.FO_DELETE;
lpFileOp.fFlags = UnsafeNativeMethods.FOF_SILENT | UnsafeNativeMethods.FOF_NOERRORUI;
if (!options.HasFlag(ShellFileDeleteOptions.NoRecycleBin))
{
lpFileOp.fFlags |= UnsafeNativeMethods.FOF_ALLOWUNDO;
}
if (!options.HasFlag(ShellFileDeleteOptions.Confirmation))
{
lpFileOp.fFlags |= UnsafeNativeMethods.FOF_NOCONFIRMATION;
}
lpFileOp.pFrom = stringBuilder.ToString();
lpFileOp.fAnyOperationsAborted = false;
lpFileOp.hNameMappings = IntPtr.Zero;
if (UnsafeNativeMethods.SHFileOperation(ref lpFileOp) != 0 || lpFileOp.fAnyOperationsAborted)
{
throw new Win32Exception();
}
}
}
}
}
}

public static void DeleteFile(ShellFileDeleteOptions options, params string[] files)
{
Expand Down

0 comments on commit a875b6c

Please sign in to comment.