mario.deghetto

Visual Basic & .NET
posts - 332, comments - 79, trackbacks - 0

#23 Cancellare tutti i file di una cartella

Per cancellare tutti i file contenuti in una cartella, possiamo utilizzare una semplice routine come questa:

Public Sub SvuotaDir(ByVal cartella As String)
   Dim di As IO.DirectoryInfo = _
      New IO.DirectoryInfo(cartella)
   For Each oFile As IO.FileInfo In di.GetFiles()
      oFile.Delete()
   Next
End Sub

E’ necessario passare alla Sub una stringa contenente il percorso della cartella da svuotare (compreso l’identificativo del drive). Ecco un esempio di utilizzo, con un pulsante nel form per avviare la cancellazione dei file:

Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button1.Click
      Dim cartella As String = “”
         cartella = “C:prova”
      SvuotaDir(cartella)
      MessageBox.Show(“Fatto!”)
   End Sub
   Public Sub SvuotaDir(ByVal cartella As String)
      Dim di As IO.DirectoryInfo = _
         New IO.DirectoryInfo(cartella)
      For Each oFile As IO.FileInfo In di.GetFiles()
         oFile.Delete()
      Next
   End Sub
End Class

Print | posted on venerdì 15 gennaio 2010 5.21 |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 1 and type the answer here:

Powered by: