Directory.Exists(String) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Bepaalt of het opgegeven pad verwijst naar een bestaande map op schijf.
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
Parameters
- path
- String
Het pad om te testen.
Retouren
true als path deze verwijst naar een bestaande map; false als de map niet bestaat of er een fout optreedt bij het bepalen of de opgegeven map bestaat.
Voorbeelden
In het volgende voorbeeld wordt een matrix met bestands- of mapnamen op de opdrachtregel gebruikt, wordt bepaald wat voor soort naam het is en wordt deze op de juiste manier verwerkt.
// 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
Opmerkingen
De path parameter mag relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap.
Volgspaties worden verwijderd uit het einde van de path parameter voordat wordt gecontroleerd of de map bestaat.
De hoofdlettergevoeligheid van de path parameter komt overeen met die van het bestandssysteem waarop de code wordt uitgevoerd. Het is bijvoorbeeld niet hoofdlettergevoelig voor NTFS (het standaardbestandssysteem Windows) en hoofdlettergevoelig op Linux-bestandssystemen.
Als u niet minimaal een alleen-lezenmachtiging voor de map hebt, wordt de Exists methode geretourneerd false.
De Exists methode retourneert false of er een fout optreedt tijdens het bepalen of het opgegeven bestand bestaat. Dit kan gebeuren in situaties waarin uitzonderingen optreden, zoals het doorgeven van een bestandsnaam met ongeldige tekens of te veel tekens, een defecte of ontbrekende schijf, of als de aanroeper niet gemachtigd is om het bestand te lezen.