File.Exists メソッド

指定したファイルが存在するかどうかを確認します。

Public Shared Function Exists( _
   ByVal path As String _) As Boolean
[C#]
public static bool Exists(stringpath);
[C++]
public: static bool Exists(String* path);
[JScript]
public static function Exists(
   path : String) : Boolean;

パラメータ

  • path
    確認するファイル。

戻り値

呼び出し元が必要なアクセス許可を持ち、 path に既存のファイル名が格納されている場合は true 。それ以外の場合は false 。このメソッドは、 path が null 参照 (Visual Basic では Nothing) または長さ 0 の文字列の場合も false を返します。呼び出し元が指定したファイルを読み取るための十分なアクセス許可を持たない場合、例外はスローされず、このメソッドは、 path の有無にかかわらず false を返します。

解説

path パラメータは、相対パス情報または絶対パス情報を指定することを許可されています。相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。現在の作業ディレクトリを取得するには、 GetCurrentDirectory のトピックを参照してください。

path がディレクトリを示す場合、このメソッドは false を返します。

このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。

実行するタスク 参考例があるトピック
テキスト ファイルを作成する。 ファイルへのテキストの書き込み
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルをコピーする。 File.Copy

FileInfo.CopyTo

ファイルの名前を変更、またはファイルを移動する。 File.Move

FileInfo.MoveTo

ファイルのサイズを取得する。 Length
バイナリ ファイルから読み取る。 新しく作成したデータ ファイルの読み取りと書き込み
バイナリ ファイルに書き込む。 新しく作成したデータ ファイルの読み取りと書き込み
サブディレクトリを作成する。 CreateSubdirectory
ディレクトリ内のファイルを参照する。 Name
ディレクトリ内のファイルをサイズ順に並べ替える。 GetFileSystemInfos
ファイルの属性を設定する。 SetAttributes

使用例

[Visual Basic, C#, C++] 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

[C#] 
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.");
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    String* path2 = String::Concat(path, S"temp");
    try {
        StreamWriter* sw = File::CreateText(path);
        if (sw) __try_cast<IDisposable*>(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(S"The target already exists");
            } else {
                // Try to copy the file.
                File::Copy(path, path2);
                Console::WriteLine(S"{0} was copied to {1}.", path, path2);
            }
        } else {
            Console::WriteLine(S"The source file does not exist.");
        }
    } catch (Exception*) {
        Console::WriteLine(S"Double copying is not allowed, as expected.");
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

.NET Framework セキュリティ:

参照

File クラス | File メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み