Directory and File - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Directory and File

Description:

... Returns an array of file names Directory.GetFiles(currDir, – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 11
Provided by: cob
Learn more at: https://faculty.sfsu.edu
Category:
Tags: directory | file

less

Transcript and Presenter's Notes

Title: Directory and File


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.SelectedIndexCh
anged 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(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?
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).ToL
ongDateString()) ListBox1.Items.Add(Fi
le.GetLastAccessTime(fName).ToLongDateString)
ListBox1.Items.Add(File.GetLastWriteTime(f
Name).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_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
Write a Comment
User Comments (0)
About PowerShow.com