Directory.Exists(String) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje, zda daná cesta odkazuje na existující adresář na disku.
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
Parametry
- path
- String
Cesta k otestování.
Návraty
true pokud path odkazuje na existující adresář; false pokud adresář neexistuje nebo dojde k chybě při pokusu o zjištění, zda zadaný adresář existuje.
Příklady
Následující příklad přebírá pole názvů souborů nebo adresářů na příkazovém řádku, určuje, o jaký druh názvu se jedná, a odpovídajícím způsobem ho zpracuje.
// 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
Poznámky
Parametr path má povoleno zadat relativní nebo absolutní informace o cestě. Relativní informace o cestě se interpretují jako relativní vzhledem k aktuálnímu pracovnímu adresáři.
Koncové mezery se odeberou z konce parametru path před kontrolou, jestli adresář existuje.
Citlivost na malá a velká písmena path parametru odpovídá systému souborů, na kterém je kód spuštěný. Například nerozlišuje malá a velká písmena v systému souborů NTFS (výchozí systém souborů Windows) a v systémech linuxových souborů rozlišují malá a velká písmena.
Pokud nemáte alespoň oprávnění jen pro čtení k adresáři, Exists metoda vrátí false.
Metoda Exists vrátí false , pokud dojde k nějaké chybě při pokusu zjistit, zda zadaný soubor existuje. K tomu může dojít v situacích, kdy vyvoláte výjimky, jako je předání názvu souboru s neplatnými znaky nebo příliš mnoho znaků, selhání nebo chybějící disk nebo pokud volající nemá oprávnění ke čtení souboru.