Skip to content

Instantly share code, notes, and snippets.

@TheWover
Created May 18, 2021 19:00
Show Gist options
  • Save TheWover/9872641aaa1cb5573bb36b4ea78a60f8 to your computer and use it in GitHub Desktop.
Save TheWover/9872641aaa1cb5573bb36b4ea78a60f8 to your computer and use it in GitHub Desktop.
Schedule a file for deletion on reboot. Lets you delete locked files and stuff in System32.
using System;
namespace NukeFile
{
class Program
{
///
/// Consts defined in WINBASE.H
///
[Flags]
internal enum MoveFileFlags
{
MOVEFILE_REPLACE_EXISTING = 1,
MOVEFILE_COPY_ALLOWED = 2,
MOVEFILE_DELAY_UNTIL_REBOOT = 4, //This value can be used only if the process is in the context of a user who belongs to the administrators group or the LocalSystem account
MOVEFILE_WRITE_THROUGH = 8
}
/// <summary>
/// Marks the file for deletion during next system reboot
/// </summary>
/// <param name="lpExistingFileName">The current name of the file or directory on the local computer.</param>
/// <param name="lpNewFileName">The new name of the file or directory on the local computer.</param>
/// <param name="dwFlags">MoveFileFlags</param>
/// <returns>bool</returns>
/// <remarks>http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx</remarks>
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "MoveFileEx")]
internal static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName,
MoveFileFlags dwFlags);
static void Main(string[] args)
{
if (MoveFileEx(args[0], null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT))
Console.WriteLine("SUCCESS! Scheduled for deletion.");
else
Console.WriteLine("FAILED! Error: {0}", System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment