directory and file. access files in a directory name space: system.io the directory and file classes...
Post on 26-Dec-2015
219 Views
Preview:
TRANSCRIPT
Directory and File
Access 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.
Directory 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")
GetDirectory 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)
File Class
• Methods:– GetCreationTime– GetLastAccessTime– GetLastWriteTime– GetAttributes
Dim currDir As String = "c"
Directory.SetCurrentDirectory(currDir)
Dim nFile As Integer
nFile = Directory.GetFiles(currDir).GetUpperBound(0)
Dim fName, fPath As String
For Each fName In Directory.GetFiles(currDir, "*.txt")
ListBox1.Items.Add(fName)
ListBox1.Items.Add(File.GetCreationTime(fName).ToLongDateString())
ListBox1.Items.Add(File.GetLastAccessTime(fName).ToLongDateString)
ListBox1.Items.Add(File.GetLastWriteTime(fName).ToLongDateString)
Next
TreeView Control
• Drill Down interface• Displays information in a hierarchical manner.• Nodes collection:
– Root: No parent
– Other nodes have a single parent.
– Siblings: Children have the same parent.• First Sibling, Last Sibling
• TreeNode Editor
TreeView Properties
• TopNode
• Nodes– Nodes(Index)
• GetNodeCount(False)
• SelectedNode– FullPath
Display Root Node’s Children
Dim TrNode As TreeNode
For Each TrNode In TreeView1.TopNode.Nodes
MessageBox.Show(TrNode.Text)
MessageBox.Show(TrNode.FullPath)
Nextree
Note: Nodes is a collection of children nodes directly belong to a parent node.
Displaying Selected Node’s Information
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
MessageBox.Show(TreeView1.SelectedNode.Text)
MessageBox.Show(TreeView1.SelectedNode.FullPath())
End Sub
Adding a TreeNode Programmatically
• Nodes collection’s Add method:– Add(string)
• TreeView1.Nodes.Add("Root")
– Add(a tree node)• Define a tree node object
• Dim myNode As New TreeNode()
• myNode.Text = "Root"
• TreeView1.Nodes.Add(myNode)
TreeView1.Nodes.Add("Root")
TreeView1.Nodes(0).Nodes.Add("Child1")
TreeView1.Nodes(0).Nodes.Add("Child2")
TreeView1.Nodes(0).Nodes.Add("child3")
TreeView1.Nodes(0).Nodes(0).Nodes.Add("GrandC11")
TreeView1.Nodes(0).Nodes(0).Nodes.Add("GrandC12")
TreeView1.Nodes(0).Nodes(1).Nodes.Add("GrandC21")
TreeView1.Nodes(0).Nodes(2).Nodes.Add("GrandC31")
TreeView1.Nodes(0).Nodes(2).Nodes.Add("GrandC32")
TreeView1.Nodes(0).Nodes(2).Nodes(0).Nodes.Add("GGrandC311")
Recursive Procedure
• A procedure that calls itself.
Creating a TreeView of DirectoriesPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DiskDrive As String = "C:\"
TreeView1.Nodes.Add(DiskDrive)
Call GetFolders(TreeView1.Nodes(0))
End Sub
Private Sub GetFolders(ByVal CurrentFolder As TreeNode)
Dim FolderPath As String
FolderPath = CurrentFolder.Text
Dim folder As String
If Directory.GetDirectories(FolderPath).Length <> 0 Then
For Each folder In Directory.GetDirectories(FolderPath)
Dim newNode As New TreeNode(folder)
CurrentFolder.Nodes.Add(newNode)
Call GetFolders(newNode)
Next
End If
End Sub
Displaying Files in Selected Folder
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim currDir As String
currDir = TreeView1.SelectedNode.Text
Dim fName As String
ListBox1.Items.Clear()
For Each fName In Directory.GetFiles(currDir)
ListBox1.Items.Add(fName)
Next
End Sub
top related