如何查找网络路径是否可用

时间:2009-07-16 05:49:08

标签: vb6

如何查找目录是否可用?

使用VB 6.0

databasetext =可用的网络路径

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

我从网络路径中选择文件,如果网络路径不可用,则显示错误“文件名或数字错误”

如何解决这个问题?

需要VB 6代码帮助

2 个答案:

答案 0 :(得分:2)

我使用PathIsDirectory中的Shlwapi.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 Scripting Runtime(包括FileSystemObject)。根据我的经验,它偶尔会在用户计算机上被破坏,可能是因为他们的IT部门对病毒感到偏执。

答案 1 :(得分: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