方法一:
在編程時經常會用到判斷文件是否存在,比如對文件做讀寫操作前,或是判斷密鑰文件是否存在等。判斷的方法有很多,有些方法雖很實用,但有點繁瑣。其實還可以有更簡單的方法,就是使用vb 6.0提供的filesystemobject對象。
filesystemobject對象不是vb內置對象,使用前必須首先選擇[工程]→[引用],在出現的窗口中選擇“microsoft scripting runtime”,然后利用filesystemobject的fileexists方法來判斷文件是否存在。示例程序代碼如下:
private sub command1_click()
′引用filesystemobject對象
dim fs as new filesystemobject
′利用filesystemobject對象的fileexists
′方法判斷文件是否存在
if fs.fileexists(″c:*.gif″) then
msgbox ″文件存在″
else
msgbox ″文件不存在″
end if
end sub
靈活運用filesystemobject對象可以解決與文件操作有關的大部分問題。
方法二:
1。利用DIR
If dir(fname)="" then '文件不存在
2。利用 api
在某些場合,我們需要確定特定目錄下特定文件是否存在。VB自帶的DIR函數可以查找符合條件的文件。這里介紹一種較為簡單的方法。
API函數的 SHFileExists 的功能,從其名字來看,應該是 Search File Exists,亦即查找存在的文件。用它來檢測文件存在與否是很容易的。試看下面的例子。
在標准EXE工程放置兩個文本框和一個按鈕,輸入如下代碼:
Private Declare Function SHFileExists Lib "shell32" Alias "#45" (ByVal szPath As String) As Long
Private Sub Command1_Click()
Dim i As Integer
i = Str$(SHFileExists(Text1.Text))
If i = 0 Then 'Str$值只有兩種可能,0或者1
Text2.Text = "文件不存在"
Else
Text2 = "文件存在"
End If
End Sub
按F5運行程序,在 Text1 輸入要查找的文件的驅動器名、路徑和名稱,然后點擊按鈕,Text2會報告文件是否存在。
值得一提的是,SHFileExists 函數支持對任何文件的查找,同時也支持對文件夾的查找。
3。Public Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath As String) As Long '創建多層目錄
用法:
MakeSureDirectoryPathExists "c:/this/is/a/test/directory/"
4。不用FSO對象 VB直接檢測文件是否存在
'不用FSO對象 VB直接檢測文件是否存在,當使用fso的程序需要帶runtime文件。' 這樣程序變成多個文件,很多操作系統本身並沒有這個文件。'有些人使用Dir("文件名")判斷,但是當主調函數也正在用dir並且后續使用沒有結束時就會出錯。Public Function FileExists(ByVal File As String) As Boolean
On Error Resume Next
If (GetAttr(File) And vbDirectory) = False Then FileExists = True
If err Then FileExists = False: err.Clear
End Function
Function FolderExists(ByVal Folder As String) As Boolean
On Error Resume Next
If GetAttr(Folder) And vbDirectory Then FolderExists = True
If err Then FolderExists = False: err.Clear
End Function
上面都是用的vbDirectory=16 不要認為寫錯了
1。利用DIR
If dir(fname)="" then '文件不存在
2。利用 api
在某些場合,我們需要確定特定目錄下特定文件是否存在。VB自帶的DIR函數可以查找符合條件的文件。這里介紹一種較為簡單的方法。
API函數的 SHFileExists 的功能,從其名字來看,應該是 Search File Exists,亦即查找存在的文件。用它來檢測文件存在與否是很容易的。試看下面的例子。
在標准EXE工程放置兩個文本框和一個按鈕,輸入如下代碼:
Private Declare Function SHFileExists Lib "shell32" Alias "#45" (ByVal szPath As String) As Long
Private Sub Command1_Click()
Dim i As Integer
i = Str$(SHFileExists(Text1.Text))
If i = 0 Then 'Str$值只有兩種可能,0或者1
Text2.Text = "文件不存在"
Else
Text2 = "文件存在"
End If
End Sub
按F5運行程序,在 Text1 輸入要查找的文件的驅動器名、路徑和名稱,然后點擊按鈕,Text2會報告文件是否存在。
值得一提的是,SHFileExists 函數支持對任何文件的查找,同時也支持對文件夾的查找。
3。Public Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal DirPath As String) As Long '創建多層目錄
用法:
MakeSureDirectoryPathExists "c:/this/is/a/test/directory/"
4。不用FSO對象 VB直接檢測文件是否存在
'不用FSO對象 VB直接檢測文件是否存在,當使用fso的程序需要帶runtime文件。' 這樣程序變成多個文件,很多操作系統本身並沒有這個文件。'有些人使用Dir("文件名")判斷,但是當主調函數也正在用dir並且后續使用沒有結束時就會出錯。Public Function FileExists(ByVal File As String) As Boolean
On Error Resume Next
If (GetAttr(File) And vbDirectory) = False Then FileExists = True
If err Then FileExists = False: err.Clear
End Function
Function FolderExists(ByVal Folder As String) As Boolean
On Error Resume Next
If GetAttr(Folder) And vbDirectory Then FolderExists = True
If err Then FolderExists = False: err.Clear
End Function
上面都是用的vbDirectory=16 不要認為寫錯了
出處:https://blog.csdn.net/lbuskeep/article/details/6317658