2009-06-17 7 views
6

Comment procéder pour marquer un dossier à supprimer lors du redémarrage du système, en utilisant C#.Comment marquer des dossiers pour la suppression C#

Merci,

+1

En général, je ne savais pas que Windows avait cette capacité du tout, beaucoup moins en C#. Comment feriez-vous cela en natif Win32? –

+0

Voir la réponse ci-dessous - c'est une API Win32 qui est appelée depuis C# via P/Invoke. –

+0

@Jason: Cool, merci. Je savais que MoveFile pouvait être différé, mais je ne savais pas qu'il pouvait être utilisé pour la suppression. –

Répondre

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

Pour complimenter une réponse par ailleurs bonne, '[Flags]' est manquant sur 'MoveFileFlags'. +1 – MickyD

+0

L'utilisation de MOVEFILE_DELAY_UNTIL_REBOOT avec MoveFileEx supprime uniquement les dossiers vides. http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240%28v=vs.85%29.aspx. Donc, vous devez supprimer et définir pour supprimer tous les fichiers et sous-dossiers dans le dossier et seulement ensuite supprimer le dossier. –

3

Utilisez PInvoke et appeler MoveFileEx, en passant nulle dans la destination ....

This link a quelques exemples de code:

[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

cité 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); 

modifier: battu

0

Le problème utilise MOVEFILE_DELAY_UNTIL_REBOOT avec MoveFileEx ne supprime les dossiers vides. Reference to documentation.

Ma solution est la suivante: chaque fichier dans le répertoire est « supprimé » avec MoveFileEx avec MOVEFILE_DELAY_UNTIL_REBOOT, puis le répertoire est « supprimé » de la même manière.

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 
} 
Questions connexes