Directory.GetFiles versus My.Computer.FileSystem.GetFiles

  • Thread starter Thread starter Devon_Nullman
  • Start date Start date
D

Devon_Nullman

Guest
Any idea why My.Computer.FileSystem.GetFiles is so much slower than Directory.GetFiles ?

Sample Code:

Option Strict On
Imports System.IO

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SW As New Stopwatch
Dim TFolder As String = Environment.GetEnvironmentVariable("TEMP")
Dim TSubFolder As String = Path.GetRandomFileName
Dim DestnFolder As String = Path.Combine(TFolder, TSubFolder)
Dim FileArray() As String
Dim FileList As New List(Of String)
Dim FillTime As Decimal
Dim DirTime As Decimal
Dim MyCompTime As Decimal
Dim HowMany As String = "10000"
Dim FillCount As Integer = 10000
If MessageBox.Show(Me, "OK to create temp folder (Y/N)?" & vbNewLine & DestnFolder,
"Choose", MessageBoxButtons.YesNo) = DialogResult.Yes Then
HowMany = InputBox("How Many Files?", "Enter a number from 100 to 100000", "10000")
If Not Integer.TryParse(HowMany, FillCount) Then Exit Sub
TextBox1.Text = "Creating " & DestnFolder & vbNewLine
SW.Reset()
SW.Start()
Directory.CreateDirectory(DestnFolder)
For I = 1 To FillCount
File.WriteAllText(Path.Combine(DestnFolder, Path.GetRandomFileName), "This is a test.")
Next
SW.Stop()
FillTime = CDec(SW.ElapsedMilliseconds) / 1000
TextBox1.AppendText("Directory filled with " & FillCount.ToString &
" files in " & FillTime.ToString & " Seconds" & vbNewLine)
SW.Reset()
SW.Start()
FileArray = Directory.GetFiles(DestnFolder, "*.*")
SW.Stop()
DirTime = CDec(SW.ElapsedMilliseconds) / 1000
TextBox1.AppendText("Directory.getfiles took " & DirTime.ToString & " Seconds" & vbNewLine)
SW.Reset()
SW.Start()
FileList.AddRange(My.Computer.FileSystem.GetFiles(DestnFolder,
FileIO.SearchOption.SearchTopLevelOnly, "*.*"))
SW.Stop()
MyCompTime = CDec(SW.ElapsedMilliseconds) / 1000
TextBox1.AppendText("My.computer.filesystem.getfiles took " &
MyCompTime.ToString & " Seconds" & vbNewLine)
Directory.Delete(DestnFolder, True)
TextBox1.AppendText("Deleting " & DestnFolder & vbNewLine)
TextBox1.AppendText("Finished........" & vbNewLine)
TextBox1.AppendText("Ratio = " & (MyCompTime / DirTime).ToString("N3") & ":1")
Else
TextBox1.AppendText("Program Aborted = Press ENTER")
End If
End Sub
End Class
My test results:
Creating C:\Users\devon\AppData\Local\Temp\vcy21knh.iri
Directory filled with 20000 files in 32.851 Seconds
Directory.getfiles took 0.04 Seconds
My.computer.filesystem.getfiles took 7.462 Seconds
Deleting C:\Users\devon\AppData\Local\Temp\vcy21knh.iri
Finished........
Ratio = 186.550:1

Continue reading...
 
Back
Top