Thursday, March 19, 2009

How to delete files and folder from network drive

Hi, In this sample I will show you how to delete files and folder from your hard drive using VB.NET code.

Let me first explain in words how this will work

DELETE FILES

1. I will get all see if direcoty exists
2. I will delete all files inside "AdnanKhan" directory

Dim sPathTok As String
sPathTok = My.Settings.FilePathName
If IO.Directory.Exists(sPathTok) Then
Dim dir As DirectoryInfo = New DirectoryInfo(sPathTok)
Dim files As FileInfo() = dir.GetFiles
For Each f As FileInfo In files
If f.Name.StartsWith("AdnanKhan") Then
f.Delete()
End If
Next

myLog.WriteLog("Processed: Deleted")

Else
myLog.WriteLog("Processed: Directory Does Not Exists")
End If


'Delete all Directories
Dim sPathDir As String
sPathDir = My.Settings.FilePathNameDir
If IO.Directory.Exists(sPathDir) Then
Dim dir As DirectoryInfo = New DirectoryInfo(sPathDir)
Dim dirs As DirectoryInfo() = dir.GetDirectories
For Each d As DirectoryInfo In dirs
If d.Name.StartsWith("AK") Then
d.Delete(True)
End If
Next
myLog.WriteLog("Process #1: Directories Deleted")
Else
myLog.WriteLog("Process #1: Directory Does Not Exists")
End If

Thank You

No comments:

Post a Comment