|
-
Dec 20th, 2007, 10:56 AM
#1
Thread Starter
New Member
Re: FSO: delete dir, its files and subdirs & write list to text file
I fixed it... I thought I had referenced the library, but hadn't.
Looks like it works perfectly! Thanks!
FYI... I did change something in the last sub routine "DeleteFolder". There was a call to DeleteFiles. I changed it to call to DeleteFolder.
-
Dec 20th, 2007, 11:33 AM
#2
Thread Starter
New Member
Re: FSO: delete dir, its files and subdirs & write list to text file
UURRRGGGG! Wouldn't you know it... I was writing the program for Windows Mobile 2003 - I have to use .NET!!
I have never programmed in .NET before. I hear it is very different. How much will this change the code?
-
Dec 20th, 2007, 12:14 PM
#3
Re: FSO: delete dir, its files and subdirs & write list to text file
 Originally Posted by tuonela
UURRRGGGG! Wouldn't you know it... I was writing the program for Windows Mobile 2003 - I have to use .NET!!
I have never programmed in .NET before. I hear it is very different. How much will this change the code? 
Structurally it shouldn't change it much. You'll have to convert the VB6 file handling (Open, Print, etc.) to either use the FSO or .NET file handling. Take the code I gave you and post it in the .NET area of the forum -- I'm sure you'll get some help over there.
-
Dec 20th, 2007, 01:58 PM
#4
Thread Starter
New Member
Re: FSO: delete dir, its files and subdirs & write list to text file
Great! Thanks so much for your help with this! It was very cool to see it work on my desktop machine!
Last edited by tuonela; Dec 20th, 2007 at 02:17 PM.
Reason: clarification
-
Dec 31st, 2007, 04:29 PM
#5
Thread Starter
New Member
Re: FSO: delete dir, its files and subdirs & write list to text file
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|