更新 : 2007 年 11 月
名前空間 :
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
戻り値
型 :
System..::.Boolean
呼び出し元が必要なアクセス許可を持ち、path に既存のファイル名が格納されている場合は true。それ以外の場合は false。path が nullNothingnullptrnull 参照 (Visual Basic では Nothing)、無効なパス、または長さ 0 の文字列の場合にも、このメソッドは false を返します。呼び出し元が指定したファイルを読み取るための十分なアクセス許可を持たない場合、例外はスローされず、このメソッドは、path の有無にかかわらず false を返します。
Exists メソッドは、パスの検証には使用できません。このメソッドは、path に指定されたファイルが存在するかどうかをチェックするだけです。Exists に無効なパスが渡されると、false が返されます。
Exists メソッドを呼び出してから、ファイルに Delete などの操作を実行するまでの間に、他のプロセスがこのファイルに対して何らかの操作を実行する可能性があることに注意してください。例に示すように、Exists メソッドとファイルに対して実行する操作を try...catch ブロック内にラップすることをお勧めします。これにより、競合が発生する可能性のある範囲を狭めることができます。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
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 3.5、2.0、1.0
XNA Framework
サポート対象 : 2.0、1.0
参照
その他の技術情報