Download presentation
Presentation is loading. Please wait.
Published byNora Sherman Modified over 9 years ago
1
Directory and File
2
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.
3
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")
4
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)
5
File Class Methods: –GetCreationTime –GetLastAccessTime –GetLastWriteTime –GetAttributes
6
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
7
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
8
TreeView Properties TopNode Nodes –Nodes(Index) GetNodeCount(False) SelectedNode –FullPath
9
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.
10
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
11
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)
12
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")
13
Recursive Procedure A procedure that calls itself.
14
Creating a TreeView of Directories Private 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
15
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.