Just thought I would post the code that finally worked for me:
WORKS: VB.NET for Compact Framework 2.0 (SmartDevices - SDK)
Code:Option Explicit On Option Strict On Imports System Imports System.IO Imports System.Security.Permissions Public Class frmDelete ' Inherit the System.Windows.Forms.Form Members Inherits System.Windows.Forms.Form 'Declare variables for looping through files and folders in a directory Dim files As Long = 0 Dim directories As Long = 0 ' Make an instance of the StreamWriter Dim oWrite As System.IO.StreamWriter Private Sub frmDelete_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Open the text file oWrite = File.CreateText("\DeleteLog.txt") ' Call the DeleteFile Sub Routine Call DeleteFile("\test.txt") ' Call the DeleteFolder Sub Routine Call DeleteFolder("\junk") ' Close the text file oWrite.Close() End Sub Private Sub DeleteFile(ByRef fileName As String) ' Make sure the file exists If System.IO.File.Exists(fileName) = True Then ' Write the file to the text file oWrite.WriteLine(fileName) ' Delete the file System.IO.File.Delete(fileName) End If End Sub Private Sub DeleteFolder(ByRef folderName As String) ' Make sure the directory exists If System.IO.Directory.Exists(folderName) = True Then ' Write the directory to the test file oWrite.WriteLine(folderName) ' Call the SubFolders Sub Routine Call SubFolders(folderName) System.IO.Directory.Delete(folderName, True) End If End Sub Private Sub SubFolders(ByRef sFolder As String) Try ' Create a new DirectoryInfo object. Dim dir As New DirectoryInfo(sFolder) ' Check to see if the directory exists If Not dir.Exists Then oWrite.WriteLine("The directory does not exist.") End If ' Call the GetFileSystemInfos method. Dim infos As FileSystemInfo() = dir.GetFileSystemInfos() ' Pass the result to the ListDirectoriesAndFiles ' method defined below. ListDirectoriesAndFiles(infos) ' Display the results to the console. oWrite.WriteLine("Directories: {0}", directories) oWrite.WriteLine("Files: {0}", files) ' Catch any exceptions Catch e As Exception oWrite.WriteLine(e.Message) End Try End Sub Sub ListDirectoriesAndFiles(ByVal FSInfo() As FileSystemInfo) ' Check the FSInfo parameter. If FSInfo Is Nothing Then Throw New ArgumentNullException("FSInfo") End If ' Iterate through each item. Dim i As FileSystemInfo For Each i In FSInfo ' Check to see if this is a DirectoryInfo object. If TypeOf i Is DirectoryInfo Then ' Add one to the directory count and add to the text file directories += 1 oWrite.WriteLine(i.Name) ' Cast the object to a DirectoryInfo object. Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo) ' Iterate through all sub-directories. ListDirectoriesAndFiles(dInfo.GetFileSystemInfos()) ' Check to see if this is a FileInfo object. ElseIf TypeOf i Is FileInfo Then ' Add one to the file count and add directory to the text file files += 1 oWrite.WriteLine(i.Name) End If Next i End Sub End Class




Reply With Quote