2009-07-16 42 views
0

如何查找目錄是否可用?如何查找網絡路徑是否可用

使用VB 6.0

databasetext =可用

If Len(Dir(databasetext)) = False Then 
MsgBox "Database Path Not Available" 
End if 

我選擇從網絡路徑的文件,如果網絡路徑不可用,則顯示錯誤「錯誤的文件名稱或編號的網絡路徑「

如何解決這個問題?

需要VB 6碼幫助

回答

1

來自我的庫存庫。我想我包括了所有需要的聲明。

 
Private Declare Function FindClose Lib "Kernel32" (ByVal hFindFile As Long) As Long 
Private Declare Function FindFirstFile Lib "Kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long 

Private Const INVALID_HANDLE_VALUE = -1 
Private Const MAX_PATH = 260 

Private Type FILETIME 
    dwLowDateTime As Long 
    dwHighDateTime As Long 
End Type 

Private Type WIN32_FIND_DATA 
    dwFileAttributes As Long 
    ftCreationTime As FILETIME 
    ftLastAccessTime As FILETIME 
    ftLastWriteTime As FILETIME 
    nFileSizeHigh As Long 
    nFileSizeLow As Long 
    dwReserved0 As Long 
    dwReserved1 As Long 
    cFileName As String * MAX_PATH 
    cAlternate As String * 14 
End Type 

Public Function FolderExists(ByVal FolderSpec As String) As Boolean 
    Dim rst As Long 
    Dim udtW32FindD As WIN32_FIND_DATA 
    Dim lngFHandle As Long 
    Dim strFolder As String 'set to FolderSpec parameter so I can change it 

    strFolder = FolderSpec 
    If Right$(strFolder, 1) <> "\" Then 
     strFolder = strFolder & "\" 
    End If 
    strFolder = strFolder & "*" 'add the wildcard allows finding share roots 

    lngFHandle = FindFirstFile(strFolder, udtW32FindD) 
    If lngFHandle INVALID_HANDLE_VALUE Then 
     Call FindClose(lngFHandle) 
     FolderExists = True 
    End If 

End Function 

2

我用PathIsDirectoryShlwapi.dll。下面是一些VB6代碼:

Private Declare Function PathIsDirectory Lib "Shlwapi" _ 
    Alias "PathIsDirectoryW" (ByVal lpszPath As Long) As Long 

Function DirExists(ByVal sDirName As String) As Boolean 
    'NB The shlwapi.dll is built into Windows 2000 and 98 and later: ' 
    ' it comes withInternet Explorer 4 on NT 4 and 95. ' 
    'NB Calling "Wide" (Unicode) version. Always available. ' 
    DirExists = (PathIsDirectory(StrPtr(Trim$(sDirName))) <> 0)  
End Function 

編輯:您還可以使用FileSystemObject的,但我更喜歡以避免Microsoft腳本運行(包括FileSystemObject的)。根據我的經驗,在用戶機器上偶爾會出現這種情況,這可能是因爲他們的IT部門對病毒有偏見。

+0

對IT破壞有意思,是關於安全還是工作安全? ;) – kenny 2009-07-20 11:02:43

相關問題