Часто задаваемые вопросы и ответы по программированию на VB
Последнее обновление: 28.03.03
Q: Как проверить наличие файла?
A1 (Vasya2000):

Private Const OF_EXIST = &H4000
Private Const OFS_MAXPATHNAME = 128

Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type

Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Function IsFileExists(fileName As String) As Boolean
Dim ofs As OFSTRUCT, nRes As Long
nRes = OpenFile(fileName, ofs, OF_EXIST)
IsFileExists = (nRes = 1)
CloseHandle (nRes)
End Function

A2 (Razum):

1. Возвращает 1(файл существует) или 0 (файла нет)

Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
MsgBox PathFileExists("c:\autoexec.bat")

2. Возвращает True(файл существует) или False(файла нет)

Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Public Function DoesFileExist(ByVal strPath As String) As Boolean
DoesFileExist = PathFileExists(strPath)
End Function
MsgBox DoesFileExist("c:\autoexec.bat")

A3 (zen):

Function IsFileFound(ByVal sFilePath as String) as Boolean
IsFileFound=(FileLen(sFilePath)<>0)
End Function

FAQ составлен по материалам Форума на Исходниках.Ру.
Составитель: Crew
Copyright © 2002 by Sources.ru. All rights reserved.