2009-06-17 14 views
6

¿Cómo voy a marcar una carpeta para su eliminación cuando el sistema se reinicia, utilizando C#.cómo marcar las carpetas para su eliminación C#

Gracias,

+1

En general, no sabía que Windows tuviera esta capacidad en absoluto, y mucho menos en C#. ¿Cómo harías esto en Win32 nativo? –

+0

Consulte la respuesta a continuación: se trata de una API de Win32 que se llama desde C# a través de P/Invoke. –

+0

@Jason: Genial, gracias. Sabía que MoveFile podría ser diferido, pero no sabía que podría usarse para borrado. –

Respuesta

18

de

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); 
+1

Para complementar una respuesta que por lo demás es buena, '[Flags]' no se encuentra en 'MoveFileFlags'. +1 – MickyD

+0

El problema es usar MOVEFILE_DELAY_UNTIL_REBOOT con MoveFileEx solo eliminará las carpetas que estén vacías. http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240%28v=vs.85%29.aspx. Por lo tanto, debe eliminar y configurar para eliminar todos los archivos y subcarpetas dentro de la carpeta y solo luego eliminar la carpeta. –

3

Uso PInvoke y llamar MoveFileEx, pasando nulo en el destino como ....

This link tiene un código de ejemplo:

[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

sido tomadas de 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); 

edición: golpeado

0

El problema está utilizando MOVEFILE_DELAY_UNTIL_REBOOT con MoveFileEx sólo eliminar las carpetas que están vacíos. Reference to documentation.

Mi solución es la siguiente: todos los archivos en el directorio es "eliminado" con MoveFileEx con MOVEFILE_DELAY_UNTIL_REBOOT, y luego el directorio está "borrado" de la misma manera.

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 
} 
Cuestiones relacionadas