如何判断一个文件是否存在?(不要以为很简单哦)
如果是判断存在,可以用dir
如果是判断是否正在使用,一般可以用错误处理,比如kill这个文件,然后出错,捕捉这个错误即可
Private Sub Command1_Click()
On Error GoTo errhandle
If Dir("c:\123.txt") <> "" Then
FileCopy "c:\123.txt", "c:\234.txt"
Kill "c:\123.txt"
FileCopy "c:\234.txt", "c:\123.txt"
Kill "c:\234.txt"
MsgBox "文件存在并且没有使用"
Exit Sub
Else
MsgBox "文件不存在"
Exit Sub
End If
errhandle:
MsgBox "文件正在使用"
End Sub
'This is an undocumented function! Returns non-zero if szPath is valid absolute UNC path. Accepts file, folder or network paths. Returns True for a relative path only if it exists in the current directory. (the name actually fits...)
Public Declare Function SHFileExists Lib "shell32" Alias "#45" (ByVal szPath As String) As Long
Private Sub Command1_Click()
If Dir("C:\", vbArchive + vbHidden + vbNormal + vbReadOnly + vbSystem) = "" Then
Print False
Else
Print True
End If
End Sub