Saturday 26 January 2013

Delete a Directory or Folder in VB.NET

Delete a Directory or Folder in VB.NET


Deleting a   folder/Directory in VB.NET Can be done by 2 ways.
1.Static Method of Directory class Delete
2.Instance method of DirectoryInfo Delete

Before that Call  using System.IO;

Calling

DeleteDirectory("c:\personal");

 

Method1   - Directory Static method 

Shared  Sub DeleteFolder(ByVal src As String)
     Try

        'true means all sub directories with content will be deleted

        'false : Only Empty Directory will be deleted.

        ' false: If directory has content recursion set to false exception will be thrown

         Directory.Delete(src,True)
     Catch ex As Exception
         Console.WriteLine(ex.Message)
     End Try
 End Sub
 

Method 2 - DirectoryInfo Instance Method

  Shared  Sub DeleteFolder(ByVal src As String)
     Try

        'true means all sub directories with content will be deleted

        'false : Only Empty Directory will be deleted.

        ' false: If directory has content recursion set to false exception will be thrown

         Dim dInfo As DirectoryInfo =  New DirectoryInfo(src)
         dInfo.Delete(True)
     Catch ex As Exception
         Console.WriteLine(ex.Message)
     End Try
  End Sub

 

 

  • Could not find a part of the path 'c:\mypersonal'. If directory mypersonal does not exists.
  • The directory is not empty.  if directory has files/folders and recursion set to false.

Tags: Delete directory in VB.NET,Delete Folder in VB.NET, Delete all files and folders in VB.NET, Directory.Delete,DirectoryInfo.Delete,System.IO. Delete Directory with files,Delete Directory with content VB.NET,Delete Directory recursively.

No comments:

Post a Comment