Directory.Exists(String) Directory.Exists(String) Directory.Exists(String) Directory.Exists(String) Method

定義

指定したパスがディスク上の既存のディレクトリを参照しているかどうかを確認します。Determines whether the given path refers to an existing directory on disk.

public:
 static bool Exists(System::String ^ path);
public static bool Exists (string path);
static member Exists : string -> bool
Public Shared Function Exists (path As String) As Boolean

パラメーター

path
String String String String

テストするパス。The path to test.

戻り値

path が既存のディレクトリを参照している場合は true。ディレクトリが存在していない場合や、指定したディレクトリが存在するかどうかを判断しようとしたときにエラーが発生した場合は falsetrue if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.

次の例では、コマンドラインでファイルまたはディレクトリ名の配列を受け取るを名前の種類は、し、適切な処理を決定します。The following example takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately.

// For File::Exists, Directory::Exists
using namespace System;
using namespace System::IO;
using namespace System::Collections;

// Insert logic for processing found files here.
void ProcessFile( String^ path )
{
   Console::WriteLine( "Processed file '{0}'.", path );
}


// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
void ProcessDirectory( String^ targetDirectory )
{
   
   // Process the list of files found in the directory.
   array<String^>^fileEntries = Directory::GetFiles( targetDirectory );
   IEnumerator^ files = fileEntries->GetEnumerator();
   while ( files->MoveNext() )
   {
      String^ fileName = safe_cast<String^>(files->Current);
      ProcessFile( fileName );
   }

   
   // Recurse into subdirectories of this directory.
   array<String^>^subdirectoryEntries = Directory::GetDirectories( targetDirectory );
   IEnumerator^ dirs = subdirectoryEntries->GetEnumerator();
   while ( dirs->MoveNext() )
   {
      String^ subdirectory = safe_cast<String^>(dirs->Current);
      ProcessDirectory( subdirectory );
   }
}

int main( int argc, char *argv[] )
{
   for ( int i = 1; i < argc; i++ )
   {
      String^ path = gcnew String(argv[ i ]);
      if ( File::Exists( path ) )
      {
         
         // This path is a file
         ProcessFile( path );
      }
      else
      if ( Directory::Exists( path ) )
      {
         
         // This path is a directory
         ProcessDirectory( path );
      }
      else
      {
         Console::WriteLine( "{0} is not a valid file or directory.", path );
      }

   }
}

// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }
        
    // Insert logic for processing found files here.
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);	    
    }
}
' For File.Exists, Directory.Exists 

Imports System
Imports System.IO
Imports System.Collections

Public Class RecursiveFileProcessor

    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim path As String
        For Each path In args
            If File.Exists(path) Then
                ' This path is a file.
                ProcessFile(path)
            Else
                If Directory.Exists(path) Then
                    ' This path is a directory.
                    ProcessDirectory(path)
                Else
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If
            End If
        Next path
    End Sub 'Main


    ' Process all files in the directory passed in, recurse on any directories 
    ' that are found, and process the files they contain.
    Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory.
        Dim fileName As String
        For Each fileName In fileEntries
            ProcessFile(fileName)

        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory.
        Dim subdirectory As String
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory

    End Sub 'ProcessDirectory

    ' Insert logic for processing found files here.
    Public Shared Sub ProcessFile(ByVal path As String)
        Console.WriteLine("Processed file '{0}'.", path)
    End Sub 'ProcessFile
End Class 'RecursiveFileProcessor

注釈

path相対または絶対パス情報を指定するパラメーターは許可されています。The path parameter is permitted to specify relative or absolute path information. 相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。Relative path information is interpreted as relative to the current working directory.

末尾から末尾のスペースが削除、pathディレクトリが存在するかどうかを確認する前にパラメーター。Trailing spaces are removed from the end of the path parameter before checking whether the directory exists.

pathパラメーター小文字は区別されません。The path parameter is not case-sensitive.

ディレクトリに、最小の読み取り専用権限があるない場合、Existsメソッドが返すfalseします。If you do not have at a minimum read-only permission to the directory, the Exists method will return false.

Existsメソッドを返します。falseを、指定したファイルが存在する場合の確認中にエラーが発生した場合。The Exists method returns false if any error occurs while trying to determine if the specified file exists. これは無効な文字または文字が多すぎます、失敗したか、不足しているディスクとファイル名を渡すなどの例外が発生する状況で発生または呼び出し元がファイルの読み取りアクセス許可を持っていない場合。This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

セキュリティ

FileIOPermission
ファイルの読み取り用for reading files. 関連付けられた列挙体。 ReadAssociated enumeration: Read

適用対象

こちらもご覧ください