DirectoryInfo.Exists DirectoryInfo.Exists DirectoryInfo.Exists DirectoryInfo.Exists Property

定義

取得值,指出目錄是否存在。Gets a value indicating whether the directory exists.

public:
 virtual property bool Exists { bool get(); };
public override bool Exists { get; }
member this.Exists : bool
Public Overrides ReadOnly Property Exists As Boolean

屬性值

如果目錄存在,則為 true,否則為 falsetrue if the directory exists; otherwise, false.

範例

下列範例示範在將來源目錄複寫Exists到目標目錄的內容中,使用屬性的方式。The following example demonstrates a use of the Exists property in the context of copying a source directory to a target directory.

using namespace System;
using namespace System::IO;

// Copy a source directory to a target directory.
void CopyDirectory( String^ SourceDirectory, String^ TargetDirectory )
{
   DirectoryInfo^ source = gcnew DirectoryInfo( SourceDirectory );
   DirectoryInfo^ target = gcnew DirectoryInfo( TargetDirectory );
   
   //Determine whether the source directory exists.
   if (  !source->Exists )
      return;

   if (  !target->Exists )
      target->Create();

   
   //Copy files.
   array<FileInfo^>^sourceFiles = source->GetFiles();
   for ( int i = 0; i < sourceFiles->Length; ++i )
      File::Copy( sourceFiles[ i ]->FullName, String::Concat( target->FullName, "\\", sourceFiles[ i ]->Name ), true );
   
   //Copy directories.
   array<DirectoryInfo^>^sourceDirectories = source->GetDirectories();
   for ( int j = 0; j < sourceDirectories->Length; ++j )
      CopyDirectory( sourceDirectories[ j ]->FullName, String::Concat( target->FullName, "\\", sourceDirectories[ j ]->Name ) );
}

int main()
{
   CopyDirectory( "D:\\Tools", "D:\\NewTools" );
}

using System;
using System.IO;

namespace DirectoryInfoCS2
{
    class Class1
    {
        // Copy a source directory to a target directory.
        static public void CopyDirectory(string SourceDirectory, string TargetDirectory)
        {
            DirectoryInfo	source = new DirectoryInfo(SourceDirectory);
            DirectoryInfo	target = new DirectoryInfo(TargetDirectory);
			
            //Determine whether the source directory exists.
            if(!source.Exists)
                return;
            if(!target.Exists)
                target.Create();
			
            //Copy files.
            FileInfo[] sourceFiles = source.GetFiles();	
            for(int i = 0; i < sourceFiles.Length; ++i)
                File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name,true);
			
            //Copy directories.
            DirectoryInfo[] sourceDirectories = source.GetDirectories();	
            for(int j = 0; j < sourceDirectories.Length; ++j)
                CopyDirectory(sourceDirectories[j].FullName,target.FullName +"\\" + sourceDirectories[j].Name);
        }
        
        static void Main(string[] args)
        {
            CopyDirectory("D:\\Tools","D:\\NewTools");
        }
    }
}
Imports System.IO

Module Module1
    Public Sub CopyDirectory(ByVal SourceDirectory As String, ByVal TargetDirectory As String)
        Dim source As DirectoryInfo = New DirectoryInfo(SourceDirectory)
        Dim target As DirectoryInfo = New DirectoryInfo(TargetDirectory)

        'Determine whether the source directory exists.
        If (source.Exists = False) Then
            Return
        End If
        If (target.Exists = False) Then
            target.Create()
        End If

        'Copy files.
        Dim sourceFiles As FileInfo() = source.GetFiles()
        Dim i, j As Integer
        For i = 0 To sourceFiles.Length - 1
            File.Copy(sourceFiles(i).FullName, target.FullName + "\\" + sourceFiles(i).Name, True)
        Next i

        'Copy directories.
        Dim sourceDirectories As DirectoryInfo() = source.GetDirectories()
        For j = 0 To sourceDirectories.Length - 1
            CopyDirectory(sourceDirectories(j).FullName, target.FullName + "\\" + sourceDirectories(j).Name)
        Next j
        source = Nothing
        target = Nothing
    End Sub
    Sub Main()
        CopyDirectory("D:\\Tools", "D:\\NewTools")
    End Sub
End Module

備註

如果在嘗試判斷指定的檔案是否存在時發生任何錯誤,則Exists屬性會傳回。 falseThe Exists property 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.

適用於

另請參閱