Directory.GetFileSystemEntries メソッド (String)
指定したディレクトリ内のすべてのファイル名とサブディレクトリ名を返します。
Overloads Public Shared Function GetFileSystemEntries( _
ByVal path As String _) As String()
[C#]
public static string[] GetFileSystemEntries(stringpath);
[C++]
public: static String* GetFileSystemEntries(String* path) __gc[];
[JScript]
public static function GetFileSystemEntries(
path : String) : String[];
パラメータ
- path
ファイル名とサブディレクトリ名が返されるディレクトリ。
戻り値
指定したディレクトリ内のファイル システム エントリ名を格納する String 配列。
例外
例外の種類 | 条件 |
---|---|
UnauthorizedAccessException | 呼び出し元に、必要なアクセス許可がありません。 |
ArgumentException | path が、長さが 0 の文字列であるか、空白しか含んでいないか、または InvalidPathChars で定義されている無効な文字を 1 つ以上含んでいます。 |
ArgumentNullException | path が null 参照 (Visual Basic では Nothing) です。 |
PathTooLongException | 指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。たとえば、Windows ベースのプラットフォームの場合、パスの長さは 248 文字未満、ファイル名の長さは 260 文字未満である必要があります。 |
IOException | path はファイル名です。 |
DirectoryNotFoundException | 割り当てられていないドライブであるなど、指定されたパスが無効です。 |
解説
このメソッドは、検索パターンとしてアスタリスク (*) を指定した GetFileSystemEntries(String, String) とまったく同じです。
path パラメータは、相対パス情報または絶対パス情報を指定することを許可されています。相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。現在の作業ディレクトリを取得するには、 GetCurrentDirectory のトピックを参照してください。
path パラメータでは大文字と小文字が区別されません。
このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
実行するタスク | 参考例があるトピック |
---|---|
テキスト ファイルを作成する。 | ファイルへのテキストの書き込み |
テキスト ファイルに書き込む。 | ファイルへのテキストの書き込み |
テキスト ファイルから読み取る。 | ファイルからのテキストの読み取り |
ディレクトリをコピーする。 | Directory |
ディレクトリの名前を変更、またはディレクトリを移動する。 | Directory.Move |
ファイルの名前を変更、またはファイルを移動する。 | File.Move |
ファイルを削除する。 | File.Delete |
ディレクトリを削除する。 | Directory.Delete |
ディレクトリを作成する。 | CreateDirectory |
サブディレクトリを作成する。 | CreateSubdirectory |
ディレクトリ内のファイルを参照する。 | Name |
ディレクトリ内のサブディレクトリを参照する。 | GetDirectories |
ディレクトリ内のすべてのサブディレクトリにあるすべてのファイルを参照する。 | GetFileSystemInfos |
ディレクトリのサイズを取得する。 | Directory |
ファイルが存在するかどうかを判別する。 | Exists |
ディレクトリが存在するかどうかを判別する。 | Exists |
ディレクトリ内のファイルをサイズ順に並べ替える。 | GetFileSystemInfos |
ファイルの属性を設定する。 | SetAttributes |
ファイルの属性を取得する。 | GetAttributes |
使用例
Option Explicit On
Option Strict On
Imports System
Namespace GetFileSystemEntries
Class Class1
Overloads Shared Sub Main(ByVal args() As String)
Dim snippets As New Class1()
Dim path As String = System.IO.Directory.GetCurrentDirectory()
Dim filter As String = "*.exe"
snippets.PrintFileSystemEntries(path)
snippets.PrintFileSystemEntries(path, filter)
snippets.GetLogicalDrives()
snippets.GetParent(path)
snippets.Move("C:\proof", "C:\Temp")
End Sub 'Main
Sub PrintFileSystemEntries(ByVal path As String)
Try
' Obtain the file system entries in the directory path.
Dim directoryEntries As String()
directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
Sub PrintFileSystemEntries(ByVal path As String, _
ByVal pattern As String)
Try
' Obtain the file system entries in the directory
' path that match the pattern.
Dim directoryEntries As String()
directoryEntries = _
System.IO.Directory.GetFileSystemEntries(path, pattern)
Dim str As String
For Each str In directoryEntries
System.Console.WriteLine(str)
Next str
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.DirectoryNotFoundException
System.Console.WriteLine("The path encapsulated in the " + _
"Directory object does not exist.")
End Try
End Sub
' Print out all logical drives on the system.
Sub GetLogicalDrives()
Try
Dim drives As String()
drives = System.IO.Directory.GetLogicalDrives()
Dim str As String
For Each str In drives
System.Console.WriteLine(str)
Next str
Catch exp As System.IO.IOException
System.Console.WriteLine("An I/O error occurs.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
End Try
End Sub
Sub GetParent(ByVal path As String)
Try
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(path)
System.Console.WriteLine(directoryInfo.FullName)
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, or " + _
"contains invalid characters.")
End Try
End Sub
Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
Try
System.IO.Directory.Move(sourcePath, destinationPath)
System.Console.WriteLine("The directory move is complete.")
Catch exp As ArgumentNullException
System.Console.WriteLine("Path is a null reference.")
Catch exp As System.Security.SecurityException
System.Console.WriteLine("The caller does not have the " + _
"required permission.")
Catch exp As ArgumentException
System.Console.WriteLine("Path is an empty string, " + _
"contains only white spaces, " + _
"or contains invalid characters.")
Catch exp As System.IO.IOException
System.Console.WriteLine("An attempt was made to move a " + _
"directory to a different " + _
"volume, or destDirName " + _
"already exists.")
End Try
End Sub
End Class
End Namespace
[C#]
using System;
namespace GetFileSystemEntries
{
class Class1
{
static void Main(string[] args)
{
Class1 snippets = new Class1();
string path = System.IO.Directory.GetCurrentDirectory();
string filter = "*.exe";
snippets.PrintFileSystemEntries(path);
snippets.PrintFileSystemEntries(path, filter);
snippets.GetLogicalDrives();
snippets.GetParent(path);
snippets.Move("C:\\proof", "C:\\Temp");
}
void PrintFileSystemEntries(string path)
{
try
{
// Obtain the file system entries in the directory path.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
void PrintFileSystemEntries(string path, string pattern)
{
try
{
// Obtain the file system entries in the directory
// path that match the pattern.
string[] directoryEntries =
System.IO.Directory.GetFileSystemEntries(path, pattern);
foreach (string str in directoryEntries)
{
System.Console.WriteLine(str);
}
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.DirectoryNotFoundException)
{
System.Console.WriteLine("The path encapsulated in the " +
"Directory object does not exist.");
}
}
// Print out all logical drives on the system.
void GetLogicalDrives()
{
try
{
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives)
{
System.Console.WriteLine(str);
}
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An I/O error occurs.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
}
void GetParent(string path)
{
try
{
System.IO.DirectoryInfo directoryInfo =
System.IO.Directory.GetParent(path);
System.Console.WriteLine(directoryInfo.FullName);
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, or " +
"contains invalid characters.");
}
}
void Move(string sourcePath, string destinationPath)
{
try
{
System.IO.Directory.Move(sourcePath, destinationPath);
System.Console.WriteLine("The directory move is complete.");
}
catch (ArgumentNullException)
{
System.Console.WriteLine("Path is a null reference.");
}
catch (System.Security.SecurityException)
{
System.Console.WriteLine("The caller does not have the " +
"required permission.");
}
catch (ArgumentException)
{
System.Console.WriteLine("Path is an empty string, " +
"contains only white spaces, " +
"or contains invalid characters.");
}
catch (System.IO.IOException)
{
System.Console.WriteLine("An attempt was made to move a " +
"directory to a different " +
"volume, or destDirName " +
"already exists.");
}
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
class Class1 {
public:
void PrintFileSystemEntries(String * path) {
try {
// Obtain the file system entries in the directory path.
String * directoryEntries[] =
System::IO::Directory::GetFileSystemEntries(path);
for (int i = 0; i < directoryEntries->Length; i++)
{
System::Console::WriteLine(directoryEntries[i]);
}
}
catch (ArgumentNullException *) {
System::Console::WriteLine("Path is a null reference.");
}
catch (System::Security::SecurityException *) {
System::Console::WriteLine("The caller does not have the \
required permission.");
}
catch (ArgumentException *) {
System::Console::WriteLine("Path is an empty String, \
contains only white spaces, \
or contains invalid characters.");
}
catch (System::IO::DirectoryNotFoundException *) {
System::Console::WriteLine("The path encapsulated in the \
Directory object does not exist.");
}
}
void PrintFileSystemEntries(String *path, String *pattern) {
try {
// Obtain the file system entries in the directory
// path that match the pattern.
String * directoryEntries[] =
System::IO::Directory::GetFileSystemEntries(path, pattern);
for (int i = 0; i < directoryEntries->Length; i++)
{
System::Console::WriteLine(directoryEntries[i]);
}
}
catch (ArgumentNullException *) {
System::Console::WriteLine("Path is a null reference.");
}
catch (System::Security::SecurityException *) {
System::Console::WriteLine("The caller does not have the \
required permission.");
}
catch (ArgumentException *) {
System::Console::WriteLine("Path is an empty String, \
contains only white spaces, \
or contains invalid characters.");
}
catch (System::IO::DirectoryNotFoundException *) {
System::Console::WriteLine("The path encapsulated in the \
Directory object does not exist.");
}
}
// Print out all logical drives on the system.
void GetLogicalDrives() {
try {
String * drives[] = System::IO::Directory::GetLogicalDrives();
for (int i = 0; i < drives->Length; i++)
{
System::Console::WriteLine(drives[i]);
}
}
catch (System::IO::IOException *) {
System::Console::WriteLine("An I/O error occurs.");
}
catch (System::Security::SecurityException *) {
System::Console::WriteLine("The caller does not have the \
required permission.");
}
}
void GetParent(String *path) {
try {
System::IO::DirectoryInfo * directoryInfo =
System::IO::Directory::GetParent(path);
System::Console::WriteLine(directoryInfo->FullName);
}
catch (ArgumentNullException *) {
System::Console::WriteLine("Path is a null reference.");
}
catch (ArgumentException *) {
System::Console::WriteLine("Path is an empty String, \
contains only white spaces, or \
contains invalid characters.");
}
}
void Move(String *sourcePath, String *destinationPath) {
try {
System::IO::Directory::Move(sourcePath, destinationPath);
System::Console::WriteLine("The directory move is complete.");
}
catch (ArgumentNullException *) {
System::Console::WriteLine("Path is a null reference.");
}
catch (System::Security::SecurityException *) {
System::Console::WriteLine("The caller does not have the \
required permission.");
}
catch (ArgumentException *) {
System::Console::WriteLine("Path is an empty String, \
contains only white spaces, \
or contains invalid characters.");
}
catch (System::IO::IOException *) {
System::Console::WriteLine("An attempt was made to move a \
directory to a different \
volume, or destDirName \
already exists.");
}
}
};
int main()
{
Class1 * snippets = new Class1();
String *path = System::IO::Directory::GetCurrentDirectory();
String *filter = "*.exe";
snippets->PrintFileSystemEntries(path);
snippets->PrintFileSystemEntries(path, filter);
snippets->GetLogicalDrives();
snippets->GetParent(path);
snippets->Move("C:\\proof", "C:\\Temp");
return 0;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
.NET Framework セキュリティ:
- FileIOPermission (現在のディレクトリのパス情報にアクセスするために必要な許可) FileIOPermissionAccess.PathDiscovery (関連する列挙体)
参照
Directory クラス | Directory メンバ | System.IO 名前空間 | Directory.GetFileSystemEntries オーバーロードの一覧 | FileSystemInfo | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み