Check that file exists: Difference between revisions

Content added Content deleted
({{header|Visual Basic}})
Line 2,244: Line 2,244:
'end if
'end if
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
</lang>

=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
The proposed solutions for VBA and VBScript work in VB6 as well, however here's a Windows API based approach:
<lang vb>
'declarations:
Public Declare Function GetFileAttributes Lib "kernel32" _
Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Public Const INVALID_FILE_ATTRIBUTES As Long = -1
Public Const ERROR_SHARING_VIOLATION As Long = 32&

'implementation:
Public Function FileExists(ByVal Filename As String) As Boolean
Dim l As Long
l = GetFileAttributes(Filename)
If l <> INVALID_FILE_ATTRIBUTES Then
FileExists = ((l And vbDirectory) = 0)
ElseIf Err.LastDllError = ERROR_SHARING_VIOLATION Then
FileExists = True
End If
End Function
</lang>
</lang>