.NET Framework Class Library
Directory..::.GetFiles Method

Returns the names of files in a specified directory.

Overload List

  NameDescription
Public methodStatic memberSupported by the .NET Compact FrameworkSupported by the XNA FrameworkGetFiles(String)Returns the names of files in the specified directory.
Public methodStatic memberSupported by the .NET Compact FrameworkSupported by the XNA FrameworkGetFiles(String, String)Returns the names of files in the specified directory that match the specified search pattern.
Public methodStatic memberGetFiles(String, String, SearchOption)Returns the names of files in the specified directory that match the specified search pattern, using a value to determine whether to search subdirectories.
Top
See Also

Reference

Tags :


Community Content

Thomas Lee
GetFiles Returns Filepaths

GetFiles does not return file names, rather it returns either relative or absolute filepaths, depending on the form of the directory specification given. For example, assume the calling process has a working directory of "C:\TEMP" and that "C:\TEMP" contains two files: "A.Dat" and "B.Dat".

GetFiles("C:\\TEMP") will return:

"C:\TEMP\A.Dat"

"C:\TEMP\B.Dat"

GetFiles("C:") will return:

"C:A.Dat"

"C:B.Dat"

GetFiles(".") will return:

".\A.Dat"

".\B.Dat"

Tags : contentbug

Shauna Liu
getting file names instead of paths
If you want just file names not paths you can use Path.GetFileName.

More on Path.GetFileName here
http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx
Tags :

Thomas Lee
Sample Using PowerShell
<#
.SYNOPSIS
This script displays the size of a folder
.DESCRIPTION
This script is a rewrite of an MSDN sample. It
uses System.IO namespace to determine the size
of a folder and its subfolders, then displays it.
Note that the Get-DirSize function is recursive!
.NOTES
File Name : Get-FolderSize.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
This script posted to:
http://pshscripts.blogspot.com/2009/10/get-foldersizeps1.html
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
.EXAMPLE
PSH [C:\foo]: .Get-FolderSize c:\foo
The size of C:\Foo and its subdirectories is 17,577,318 bytes
#>
param (
$folder = "C:\Foo"
)
##
# Helper Function Get-DirSize
##
function Get-DirSize {
param ([system.IO.DirectoryInfo] $d)
$Size = 0;
# Add file sizes.
$fis = $d.GetFiles();
foreach ($fi in $fis){
$size += $fi.Length;
}
# Add subdirectory sizes recursively.
$dis = $d.GetDirectories()
foreach ($di in $dis) {
$Size += Get-DirSize($di)
}
return $Size
}
## End of Get-DirSize helper function 
##
# Start of Script
$d = New-Object system.IO.DirectoryInfo $folder
$size= Get-Dirsize $d
"The size of {0} and its subdirectories is {1} bytes" -f $d,$size.ToString("###,###")

Page view tracker