FileInfo.Exists Eigenschap
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.
Hiermee wordt een waarde opgehaald die aangeeft of er een bestand bestaat.
public:
virtual property bool Exists { bool get(); };
public override bool Exists { get; }
member this.Exists : bool
Public Overrides ReadOnly Property Exists As Boolean
Waarde van eigenschap
true als het bestand bestaat; false als het bestand niet bestaat of als het bestand een map is.
Voorbeelden
In het volgende codevoorbeeld wordt de Exists eigenschap gebruikt om ervoor te zorgen dat er een bestand bestaat voordat u het opent. U kunt deze techniek gebruiken om een aangepaste uitzondering te genereren wanneer het bestand niet wordt gevonden.
public byte[] OpenDataFile(string FileName)
{
// Check the FileName argument.
if (FileName == null || FileName.Length == 0)
{
throw new ArgumentNullException("FileName");
}
// Check to see if the file exists.
FileInfo fInfo = new FileInfo(FileName);
// You can throw a personalized exception if
// the file does not exist.
if (!fInfo.Exists)
{
throw new FileNotFoundException("The file was not found.", FileName);
}
// Open the file.
FileStream fStream = new FileStream(FileName, FileMode.Open);
// Create a buffer.
byte [] buffer = new byte[fStream.Length];
// Read the file contents to the buffer.
fStream.Read(buffer, 0, (int)fStream.Length);
// return the buffer.
return buffer;
}
Function OpenDataFile(ByVal FileName As String) As Byte()
' Check the FileName argument.
If FileName Is Nothing OrElse FileName.Length = 0 Then
Throw New ArgumentNullException("FileName")
End If
' Check to see if the file exists.
Dim fInfo As New FileInfo(FileName)
' You can throw a personalized exception if
' the file does not exist.
If Not fInfo.Exists Then
Throw New FileNotFoundException("The file was not found.", FileName)
End If
' Open the file.
Dim fStream As New FileStream(FileName, FileMode.Open)
' Create a buffer.
Dim buffer(fStream.Length) As Byte
' Read the file contents to the buffer.
fStream.Read(buffer, 0, Fix(fStream.Length))
' return the buffer.
Return buffer
End Function
Opmerkingen
Wanneer u het eerst aanroept, FileInfo worden gegevens over het bestand aangeroepen Refresh en in de cache opgeslagen. Bij volgende oproepen moet u bellen Refresh om de meest recente kopie van de gegevens op te halen.
De Exists eigenschap 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.