Visual Basic .NET Nightmare or Guide to upgrading a Guru.

Page 7

What City Please? 

 

Private MyDir As System.IO.Directory
Private Sub btnAssistance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssistance.Click

'IO

Dim MyDirInfo As System.IO.DirectoryInfo

MyDirInfo = MyDir.CreateDirectory("C:\MyDir\WonderBar\Cool\Sweet")

tbDirectory.Text = MyDir.Exists("C:\MyDir\WonderBar\Cool\Sweet") 'Returns True if exists

MyDir.Delete("C:\MyDir", True) 'Removes all subdirectories and files.

'FSO

MkDir("C:\MyDir\WonderBar\Cool\Sweet") 'Causes error if directory exists.

If Dir("C:\MyDir\WonderBar\Cool\Sweet", FileAttribute.Directory) <> "" Then tbDirectory.Text = tbDirectory.Text & " Directory found"

RmDir("C:\MyDir\WonderBar\Cool\Sweet")

RmDir("C:\MyDir\WonderBar\Cool")

RmDir("C:\MyDir\WonderBar")

RmDir("C:\MyDir") ' Causes error if it has sub directories and files.

End Sub

Private Sub btnAssistance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssistance.Click

Dim MyDirInfo As System.IO.DirectoryInfo

Dim MyDirs As String()

Dim n As Integer

MyDirInfo = MyDir.CreateDirectory("C:\MyDir\WonderBar\Cool\Sweet")

tbDirectory.Text = MyDir.Exists("C:\MyDir\WonderBar\Cool\Sweet") 'Returns True if exists

MyDir.CreateDirectory("C:\MyDir\Creamy") ' Creates the specified directory if it doesn't already exist

MyDirs = MyDir.GetDirectories("C:\MyDir") 'Returns all the sub directories from the directory specified.

For n = 0 To MyDirs.Length - 1

tbDirectory.Text = tbDirectory.Text & vbCrLf & MyDirs(n)

Next n

MyDir.Delete("C:\MyDir", True) 'Removes all subdirectories and files.

End Sub

MyDir.Move("C:\MyDir\Creamy", "C:\MyDir\WonderBar\Cool\Sweet\Creamy")
  • If this works as expected after creating our two directories our creamy folder should end up in our sweet folder. This works fine the first time. With all previous methods no errors are thrown if said directories already exists or doesn't exist, but here we do have to account for it with this method. If the destination folder already exists you cannot copy the directory and your code will error.  If your destination directory already exits you probably want to offer up options to your end user, such as copy over existing files or keep the ones that already exist in the destination folder.  For our example we will just offer up a list of what files exist in our folder.
  • So we are going to need another string array for our file list. We are also going to want some files in our directory.
  • So let's add this line in our declarations area to access the File object.
Private MyFile As System.IO.File
  • Here we will use a similar method to .CreateDirectory.  For our file object we are going to use .Create. This method creates a file in the specified directory and returns a FileStream object.  For our purpose we are not going to use the files, just make them so we are immediately going to close them using the returned FileStream object's .Close method. This happens in a single line of code.  See our entire sub below.

Private Sub btnAssistance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssistance.Click

Dim MyDirInfo As System.IO.DirectoryInfo

Dim MyDirs As String()

Dim MyFiles As String()

Dim n As Integer

MyDirInfo = MyDir.CreateDirectory("C:\MyDir\WonderBar\Cool\Sweet")

tbDirectory.Text = MyDir.Exists("C:\MyDir\WonderBar\Cool\Sweet") 'Returns True if exists

MyDir.CreateDirectory("C:\MyDir\Creamy") ' Creates the specified directory if it doesn't already exist

MyDirs = MyDir.GetDirectories("C:\MyDir") 'Returns all the sub directories from the directory specified.

MyFile.Create("C:\MyDir\Creamy\funny.txt").Close() ' Creates a file and then closes it

MyFile.Create("C:\MyDir\Creamy\wicked.jpg").Close() 'If the files are not closed then the Move method will generate an error as the file is still open.

MyFile.Create("C:\MyDir\Creamy\lost.doc").Close()

For n = 0 To MyDirs.Length - 1

tbDirectory.Text = tbDirectory.Text & vbCrLf & MyDirs(n)

Next n

If Not MyDir.Exists("C:\MyDir\WonderBar\Cool\Sweet\Creamy") Then

MyDir.Move("C:\MyDir\Creamy", "C:\MyDir\WonderBar\Cool\Sweet\Creamy")

Else

MyFiles = MyDir.GetFiles("C:\MyDir\Creamy")

For n = 0 To MyFiles.Length - 1

tbDirectory.Text = tbDirectory.Text & vbCrLf & MyFiles(n)

Next n

End If

'MyDir.Delete("C:\MyDir", True) 'Removes all subdirectories and files.

End Sub

  • The first time we click our Assistance button our directories  will be created and our creamy directory will be moved with the 3 files we created into it.

  • If we click the button again instead of moving the folder as it already exists, it's going to list the files in it in our textbox.  Check it all out. This should be the basics of managing files and folders.

  • Now we want to a look at DirectoryInfo Object. We already have it setup in our code, but what can we do with it?

  • It's actually what it appears to be, an object that gives information about the directory. The Info object was created from our .CreateDirectory Method. It also allows you to set information on the directory.  Let's say you wanted to update it's creation time.  You could set the new value of .CreationTime to a new date, or update it for each time your application worked with the directory. The follow line sets the .CreationTime to .Now

MyDirInfo.CreationTime = MyDirInfo.CreationTime.Now

Here your going to find all kinds of relevant information from files and folders. This should speed you right along in getting a good app going.

Page 8