FileInfo.Exists 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
파일이 있는지 여부를 나타내는 값을 가져옵니다.
public:
virtual property bool Exists { bool get(); };
public override bool Exists { get; }
member this.Exists : bool
Public Overrides ReadOnly Property Exists As Boolean
속성 값
파일이 있으면true 이고, 파일이 없거나 파일이 디렉터리이면 false 입니다.
예제
다음 코드 예제에서는 파일을 열기 전에 존재 하는 속성을 사용 Exists 합니다. 파일을 찾을 수 없는 경우 이 기술을 사용하여 사용자 지정 예외를 throw할 수 있습니다.
array<Byte>^ Openfile(String^ fileName)
{
// Check the fileName argument.
if (fileName == nullptr || fileName->Length == 0)
{
throw gcnew ArgumentNullException("fileName");
}
// Check to see if the file exists.
FileInfo^ fInfo = gcnew FileInfo(fileName);
// You can throw a personalized exception if
// the file does not exist.
if (!fInfo->Exists)
{
throw gcnew FileNotFoundException("The file was not found.",
fileName);
}
try
{
// Open the file.
FileStream^ fStream = gcnew FileStream(fileName, FileMode::Open);
// Create a buffer.
array<Byte>^ buffer = gcnew array<Byte>(fStream->Length);
// Read the file contents to the buffer.
fStream->Read(buffer, 0, (int)fStream->Length);
// return the buffer.
return buffer;
}
catch (IOException^ ex)
{
Console::WriteLine(ex->Message);
return nullptr;
}
}
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
설명
처음 호출되면 FileInfo 파일에 대한 정보를 호출 Refresh 하고 캐시합니다. 후속 호출에서는 최신 정보 복사본을 가져오기 위해 호출 Refresh 해야 합니다.
Exists 지정된 파일이 있는지 확인하는 동안 오류가 발생하면 속성이 반환 false 됩니다. 잘못된 문자 또는 너무 많은 문자로 파일 이름을 전달하거나, 실패하거나 누락된 디스크를 전달하거나, 호출자에게 파일을 읽을 수 있는 권한이 없는 경우와 같은 예외가 발생할 수 있습니다.