directory and file. access files in a directory name space: system.io the directory and file classes...
TRANSCRIPT
![Page 1: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/1.jpg)
Directory and File
![Page 2: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/2.jpg)
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.
![Page 3: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/3.jpg)
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")
![Page 4: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/4.jpg)
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)
![Page 5: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/5.jpg)
File Class
• Methods:– GetCreationTime– GetLastAccessTime– GetLastWriteTime– GetAttributes
![Page 6: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/8.jpg)
TreeView Properties
• TopNode
• Nodes– Nodes(Index)
• GetNodeCount(False)
• SelectedNode– FullPath
![Page 9: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/9.jpg)
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.
![Page 10: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/10.jpg)
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
![Page 11: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/11.jpg)
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)
![Page 12: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/12.jpg)
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")
![Page 13: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/13.jpg)
Recursive Procedure
• A procedure that calls itself.
![Page 14: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/14.jpg)
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
![Page 15: 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](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e115503460f94afd942/html5/thumbnails/15.jpg)
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