Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Shared Function Exists ( _ path As String _ ) As Boolean
Dim path As String Dim returnValue As Boolean returnValue = File.Exists(path)
public static bool Exists ( string path )
public: static bool Exists ( String^ path )
public static boolean Exists ( String path )
public static function Exists ( path : String ) : boolean
Paramètres
- path
Fichier à contrôler.
Valeur de retour
true si l'appelant a les autorisations requises et si path contient le nom d'un fichier existant ; sinon false. Cette méthode retourne également false si path est référence Null (Nothing en Visual Basic), un chemin d'accès non valide ou une chaîne de longueur nulle. Si l'appelant n'a pas les autorisations suffisantes pour lire le fichier spécifié, aucune exception n'est levée et la méthode retourne false quelle que soit l'existence de path.
NotesLa méthode Exists ne doit pas être utilisée pour la validation de chemin d'accès car elle vérifie simplement si le fichier spécifié dans path existe. Le passage d'un chemin d'accès non valide à Exists retourne false.
Sachez qu'un autre processus peut effectuer une opération sur le fichier entre le moment où vous appelez la méthode Exists et celui où vous effectuez une autre opération sur le fichier, telle que Delete. Une méthode de programmation recommandée consiste à encapsuler la méthode Exists, et les opérations que vous effectuez sur le fichier, dans un bloc try...catch comme indiqué dans l'exemple. Cela permet de limiter la portée pour les conflits potentiels. La méthode Exists permet uniquement de vous assurer que le fichier sera disponible, mais elle n'offre aucune garantie.
Le paramètre path est autorisé à spécifier des informations sur le chemin d'accès relatif ou absolu. Les informations sur le chemin d'accès relatif sont interprétées comme étant relatives au répertoire de travail en cours. Pour obtenir le répertoire de travail en cours, consultez GetCurrentDirectory.
Si path décrit un répertoire, cette méthode retourne false. Les espaces de fin sont supprimés du paramètre path avant de déterminer si le fichier existe.
ExempleL'exemple suivant utilise la méthode Exists pour vérifier qu'un fichier n'est pas substitué.
Imports System Imports System.IO Imports System.Text Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" Dim path2 As String = path + "temp" Try Dim sw As StreamWriter = File.CreateText(path) sw.Close() ' Do the Copy operation only if the first file exists ' and the second file does not. If File.Exists(path) Then If File.Exists(path2) Then Console.WriteLine("The target file already exists.") Else 'try to copy it File.Copy(path, path2) Console.WriteLine("{0} was copied to {1}.", path, path2) End If Else Console.WriteLine("The source file does not exist.") End If Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub End Class
using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; string path2 = path + "temp"; try { using (StreamWriter sw = File.CreateText(path)) {} // Only do the Copy operation if the first file exists // and the second file does not. if (File.Exists(path)) { if (File.Exists(path2)) { Console.WriteLine("The target already exists"); } else { // Try to copy the file. File.Copy(path, path2); Console.WriteLine("{0} was copied to {1}.", path, path2); } } else { Console.WriteLine("The source file does not exist."); } } catch { Console.WriteLine("Double copying is not allowed, as expected."); } } }
using namespace System; using namespace System::IO; int main() { String^ path = "c:\\temp\\MyTest.txt"; String^ path2 = String::Concat( path, "temp" ); try { StreamWriter^ sw = File::CreateText( path ); if ( sw ) delete (IDisposable^)sw; // Only do the Copy operation if the first file exists // and the second file does not. if ( File::Exists( path ) ) { if ( File::Exists( path2 ) ) { Console::WriteLine( "The target already exists" ); } else { // Try to copy the file. File::Copy( path, path2 ); Console::WriteLine( "{0} was copied to {1}.", path, path2 ); } } else { Console::WriteLine( "The source file does not exist." ); } } catch ( Exception^ ) { Console::WriteLine( "Double copying is not allowed, as expected." ); } }
import System.*; import System.IO.*; class Test { public static void main(String[] args) { String path = "c:\\temp\\MyTest.txt"; String path2 = path + "temp"; try { StreamWriter sw = File.CreateText(path); try { } finally { sw.Dispose(); } // Only do the Copy operation if the first file exists // and the second file does not. if (File.Exists(path)) { if (File.Exists(path2)) { Console.WriteLine("The target already exists"); } else { // Try to copy the file. File.Copy(path, path2); Console.WriteLine("{0} was copied to {1}.", path, path2); } } else { Console.WriteLine("The source file does not exist."); } } catch (System.Exception exp) { Console.WriteLine("Double copying is not allowed, as expected."); } } //main } //Test
Sécurité .NET Framework- FileIOPermission pour lire le fichier spécifié. Énumération associée : FileIOPermissionAccess.Read
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ