Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
Directory Class
Directory Methods
GetFiles Method
Collapse All/Expand All Collapse All
Members FilterMembers Filter
Frameworks FilterFrameworks Filter
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Directory..::.GetFiles Method

Returns the names of files in a specified directory.

  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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
GetFiles Returns Filepaths      David Rogers ... Thomas Lee   |   Edit   |   Show History

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"

getting file names instead of paths      lucky0ne ... Shauna Liu   |   Edit   |   Show History
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 What's this?: Add a tag
Flag as ContentBug
Sample Using PowerShell      Thomas Lee   |   Edit   |   Show History
<#
.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("###,###")
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker