File.Exists 方法

确定指定的文件是否存在。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public 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

参数

  • path
    要检查的文件。

返回值

如果调用方具有要求的权限并且 path 包含现有文件的名称,则为 true;否则为 false。如果 path 为 空引用(在 Visual Basic 中为 Nothing)、无效路径或零长度字符串,则此方法也将返回 false。如果调用方不具有读取指定文件所需的足够权限,则不引发异常并且该方法返回 false,这与 path 是否存在无关。

备注

不应使用 Exists 方法来验证路径,此方法仅检查 path 中指定的文件是否存在。将无效路径传递到 Exists 将返回 false

请注意,在您调用 Exists 方法和对文件执行其他操作(如 Delete)之间,其他进程可能会对文件进行一些处理。建议的编程做法是在 try...catch 块中包装 Exists 方法和对文件采取的操作,如示例中所示。这有助于缩小潜在冲突的范围。Exists 方法只能帮助确保文件是可用的,但无法保证。

允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory

如果 path 描述一个目录,则此方法返回 false。在确定文件是否存在之前,从 path 参数中移除尾随空格。

示例

下面的示例使用 Exists 方法帮助确保文件不被改写。

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

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

File 类
File 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本