Download presentation
Presentation is loading. Please wait.
Published byRoland Watkins Modified over 8 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
GetDirectory, 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.SelectedIndexChanged ListBox2.DataSource = Directory.GetFiles(ListBox1.SelectedItem) End Sub
6
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 btnGetDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDir.Click ListBox1.DataSource = Directory.GetDirectories(ListBox1.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?
7
File 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).ToLongDateString()) ListBox1.Items.Add(File.GetLastAccessTime(fName).ToLongDateString) ListBox1.Items.Add(File.GetLastWriteTime(fName).ToLongDateString) Next
9
Example SMTP –Email folder: C:\inetpub\mailroot\drop List emails in a listbox and display selected email in a textbox.
10
Private 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_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim myFile As System.IO.StreamReader myFile = System.IO.File.OpenText(ListBox1.SelectedItem) TextBox1.Text = myFile.ReadToEnd() myFile.Close() End Sub
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.