как пометить папки для удаления C #

Как я могу пометить одну папку для удаления, когда система перезагружается, используя C #.

Благодаря,

c#,windows,

6

Ответов: 4


18 ов принято

из

http://abhi.dcmembers.com/blog/2009/03/24/mark-file-for-deletion-on-reboot/

///
/// Consts defined in WINBASE.H
///
internal enum MoveFileFlags
{
    MOVEFILE_REPLACE_EXISTING = 1,
    MOVEFILE_COPY_ALLOWED = 2,
    MOVEFILE_DELAY_UNTIL_REBOOT = 4,
    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);

//Usage for marking the file to delete on reboot
MoveFileEx(fileToDelete, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);

3

Используйте PInvoke и вызовите MoveFileEx, передав null в качестве адресата ....

У этой ссылки есть пример кода:

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);

public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;

MoveFileEx(filename, null, MOVEFILE_DELAY_UNTIL_REBOOT);

1

цитата из http://abhi.dcmembers.com/blog/2009/03/24/mark-file-for-deletion-on-reboot/ :

///
/// Consts defined in WINBASE.H
///
internal enum MoveFileFlags
{
    MOVEFILE_REPLACE_EXISTING = 1,
    MOVEFILE_COPY_ALLOWED = 2,
    MOVEFILE_DELAY_UNTIL_REBOOT = 4,
    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);

//Usage for marking the file to delete on reboot
MoveFileEx(fileToDelete, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);

Редактирование: избитый


0

Проблема заключается в использовании MOVEFILE_DELAY_UNTIL_REBOOT, а MoveFileEx удаляет только пустые папки. Ссылка на документацию.

Мое решение таково: каждый файл в каталоге «удален» с MoveFileEx с MOVEFILE_DELAY_UNTIL_REBOOT, а затем каталог «удаляется» одинаково.

public class Cleanuper
{
    private void PendingDeleteDirectory(string directoryPath)
    {
        foreach (string directory in Directory.GetDirectories(directoryPath, "*", SearchOption.TopDirectoryOnly))
        {
            PendingDeleteDirectory(directory);
        }

        foreach (string file in Directory.GetFiles(directoryPath, "*", SearchOption.TopDirectoryOnly))
        {
            NativeMethods.MoveFileEx(file, null, MoveFileFlags.DelayUntilReboot);
        }
        NativeMethods.MoveFileEx(directoryPath, null, MoveFileFlags.DelayUntilReboot);
    }
}

public static class NativeMethods
{
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags);
}

[Flags]
public enum MoveFileFlags
{
    DelayUntilReboot = 0x00000004
}
C #, окна,
Похожие вопросы
Яндекс.Метрика