Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6, File Doesn't Exist, How do I handle Gracefully?

Tags:

I am testing an application that checks if a file exists across a network. In my testing, I am purposefully pulling the network plug so the file will not be found. The problem is this causes my app to go unresponsive for at least 15 seconds. I have used both the FileExists() and GetAttr() functions in VB6. Does anyone know how to fix this problem? (No, I can't stop using VB6)

Thanks, Charlie

like image 925
Charlie White Avatar asked Oct 14 '08 20:10

Charlie White


2 Answers

Unfortunately, VB doesn't make this easy, but luckily the Win32 API does, and it's quite simple to call Win32 functions from within VB.

For the LAN/WAN, you can use a combination of the following Win32 API calls to tell you whether the remote connection exists without having to deal with a network time-out:

Private Declare Function WNetGetConnection Lib "mpr.dll" Alias _
    "WNetGetConnectionA" (ByVal lpszLocalName As String, _
    ByVal lpszRemoteName As String, ByRef cbRemoteName As Long) As Long

Private Declare Function PathIsNetworkPath Lib "shlwapi.dll" Alias _
    "PathIsNetworkPathA" (ByVal pszPath As String) As Long

Private Declare Function PathIsUNC Lib "shlwapi.dll" Alias "PathIsUNCA" _
    (ByVal pszPath As String) As Long

For the Internet, you can use the Win32 API call:

Private Declare Function InternetGetConnectedState Lib "wininet.dll" _
    (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long

Const INTERNET_CONNECTION_MODEM = 1
Const INTERNET_CONNECTION_LAN = 2
Const INTERNET_CONNECTION_PROXY = 4
Const INTERNET_CONNECTION_MODEM_BUSY = 8

This VB site has more discussion on path oriented functions you can call in the Win32 API through VB.

like image 133
HTTP 410 Avatar answered Oct 03 '22 03:10

HTTP 410


use this too

Dim FlSize as long 
flsize=filelen("yourfilepath")
if err.number=53 then msgbox("file not found")
if err.number=78 then msgbox("Path Does no Exist")
like image 28
Kuuri Avatar answered Oct 03 '22 03:10

Kuuri