Title: Directory and File
1Directory and File
2Access Files in a Directory
- Name space System.IO
- The Directory and File classes contain only
shared methods that set or return information
about entries in the file system. - Shared methods can be called without
instantiating an object of that class.
3Directory Class
- Methods
- SetCurrentDirectory
- Dim currDir As String "c\inetpub\mailroot\drop"
Directory.SetCurrentDirectory(currDir) - GetDirectories Returns an array of sub directory
names in the current directory - Directory.GetDirectories(currDir)
- GetFiles Returns an array of file names
- Directory.GetFiles(currDir, ".eml")
4GetDirectory Example
Dim currDir As String "C\teaching"
Directory.SetCurrentDirectory(currDir)
Dim results As String Dim folder As
String For Each folder In
Directory.GetDirectories(currDir)
results results folder vbCrLf Next
MessageBox.Show(results)
5GetDirectory, GetFiles
Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load Dim currDir As String
"c\" ListBox1.DataSource
Directory.GetDirectories(currDir) End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexCh
anged ListBox2.DataSource
Directory.GetFiles(ListBox1.SelectedItem) End
Sub
6Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load Dim currDir As
String "c\" ListBox1.DataSource
Directory.GetDirectories(currDir) End Sub
Private Sub btnGetDir_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnGetDir.Click
ListBox1.DataSource Directory.GetDirectories(Lis
tBox1.SelectedItem) End Sub Private Sub
btnGetFiles_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles
btnGetFiles.Click ListBox2.DataSource
Directory.GetFiles(ListBox1.SelectedItem) End
Sub Note Number of subdirectories and files?
7File Class
- Methods
- GetCreationTime(ByVal path As String)
- GetLastAccessTime(ByVal path As String)
- GetLastWriteTime(ByVal path As String)
- GetAttributes(ByVal path As String)
- Copy(ByVal sourceFileName As String, ByVal
destFileName As String) - Delete(ByVal path As String)
- Move(ByVal sourceFileName As String, ByVal
destFileName As String)
8 Dim currDir As String "c\"
Directory.SetCurrentDirectory(currDir)
Dim fName As String For Each fName In
Directory.GetFiles(currDir, ".txt")
ListBox1.Items.Add(fName)
ListBox1.Items.Add(File.GetCreationTime(fName).ToL
ongDateString()) ListBox1.Items.Add(Fi
le.GetLastAccessTime(fName).ToLongDateString)
ListBox1.Items.Add(File.GetLastWriteTime(f
Name).ToLongDateString) Next
9Example
- SMTP
- Email folder
- C\inetpub\mailroot\drop
- List emails in a listbox and display selected
email in a textbox.
10Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load Dim currDir As String
"c\inetpub\mailroot\drop"
ListBox1.DataSource Directory.GetFiles(currDir)
End Sub Private Sub ListBox1_SelectedInde
xChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ListBox1.SelectedInde
xChanged Dim myFile As System.IO.StreamRea
der myFile System.IO.File.OpenText(ListB
ox1.SelectedItem) TextBox1.Text
myFile.ReadToEnd() myFile.Close() End
Sub