Directory.Exists(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定给定路径是否引用磁盘上的现有目录。
public:
static bool Exists(System::String ^ path);
public static bool Exists (string path);
public static bool Exists (string? path);
static member Exists : string -> bool
Public Shared Function Exists (path As String) As Boolean
参数
- path
- String
要测试的路径。
返回
如果 path
指向现有目录,则为 true
;如果该目录不存在或者在尝试确定指定目录是否存在时出错,则为 false
。
示例
以下示例在命令行上采用文件或目录名称数组,确定其名称类型,并对其进行适当处理。
// 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);
}
}
module RecursiveFileProcessor
open System.IO
// Insert logic for processing found files here.
let processFile path =
printfn $"Processed file '%s{path}'."
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
let rec processDirectory targetDirectory =
// Process the list of files found in the directory.
let fileEntries = Directory.GetFiles targetDirectory
for fileName in fileEntries do
processFile fileName
// Recurse into subdirectories of this directory.
let subdirectoryEntries = Directory.GetDirectories targetDirectory
for subdirectory in subdirectoryEntries do
processDirectory subdirectory
[<EntryPoint>]
let main args =
for path in args do
if File.Exists path then
// This path is a file
processFile path
elif Directory.Exists path then
// This path is a directory
processDirectory path
else
printfn $"{path} is not a valid file or directory."
0
' For File.Exists, Directory.Exists
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
' 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
' Insert logic for processing found files here.
Public Shared Sub ProcessFile(ByVal path As String)
Console.WriteLine("Processed file '{0}'.", path)
End Sub
End Class
注解
允许 path
参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。
在检查目录是否存在之前, path
将从参数末尾删除尾随空格。
参数的 path
区分大小写对应于运行代码的文件系统的区分大小写。 例如,它在 NTFS 上不区分大小写, (默认 Windows 文件系统) ,Linux 文件系统上区分大小写。
如果对目录没有最低只读权限,则 Exists 方法将返回 false
。
如果在尝试确定指定文件是否存在时发生任何错误,则 Exists 该方法将返回 false
。 在引发异常的情况下,可能会发生这种情况,例如传递包含无效字符或太多字符的文件名、磁盘失败或缺失,或者调用方没有读取文件的权限。