I am using the "File.Exists" method to test to see if a file can be located on a remote machine.
The problem is that if the machine can not be seen, the function takes between 10 and 20 minutes to return a "False" result for the check!!
If the I use the same method and the machine can be reached, but the file simply doesn't exist, then it returns "False" pretty much instantly.
And of course if the machine can be reached and the file exists, then "True" is returned instantly.
So why does it hang for so long when it can't find the machine?
Is there any better way to check for machine connectivity?
My basic goal is to know whether or not a particular computer on the network is accessible.
If I was doing this in DOS, I would just ping the machine.
But the best way I could think of in VB was just to test for the existance of a known file (in this case I am using explorer.exe in the Windows folder).
My code .Net is:
***********************************************************
Imports System
Imports System.IO
Dim varFileName As String
Dim varConnected As Boolean
varFileName = "\\123.45.67.890\cdrive\windows\explorer.exe"
If File.Exists(varFileName) Then
varConnected = True
Else
varConnected = False
End If
***********************************************************
I am converting a project from VB6 to VB.Net
In the VB6 project, I used the CreateFile API from kernel32.dll as such:
***********************************************************
Public Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Dim hfile As Long
Dim varConnected As Boolean
Const GENERIC_READ = &H80000000
Const FILE_SHARE_READ = &H1
Const OPEN_EXISTING = 3
Const FILE_ATTRIBUTE_ARCHIVE = &H20
hfile = -1
hfile = CreateFile("\\123.45.67.890\cdrive\windows\explorer.exe", GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hfile = -1 Then
varConnected = False
Else
varConnected = True
End If
***********************************************************
I thought that since I was moving to .Net I would take advantage of using the simpler built in function to check for files.
But where the CreateFile API would return results regardless of connectivity within a couple seconds, the built-in File.Exists takes forever.
Before I go back and modify my code to use the API again, I would appreciate any ideas on what may be wrong, or any better suggestions on how to accomplish my goal.
Thanks.
The problem is that if the machine can not be seen, the function takes between 10 and 20 minutes to return a "False" result for the check!!
If the I use the same method and the machine can be reached, but the file simply doesn't exist, then it returns "False" pretty much instantly.
And of course if the machine can be reached and the file exists, then "True" is returned instantly.
So why does it hang for so long when it can't find the machine?
Is there any better way to check for machine connectivity?
My basic goal is to know whether or not a particular computer on the network is accessible.
If I was doing this in DOS, I would just ping the machine.
But the best way I could think of in VB was just to test for the existance of a known file (in this case I am using explorer.exe in the Windows folder).
My code .Net is:
***********************************************************
Imports System
Imports System.IO
Dim varFileName As String
Dim varConnected As Boolean
varFileName = "\\123.45.67.890\cdrive\windows\explorer.exe"
If File.Exists(varFileName) Then
varConnected = True
Else
varConnected = False
End If
***********************************************************
I am converting a project from VB6 to VB.Net
In the VB6 project, I used the CreateFile API from kernel32.dll as such:
***********************************************************
Public Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Dim hfile As Long
Dim varConnected As Boolean
Const GENERIC_READ = &H80000000
Const FILE_SHARE_READ = &H1
Const OPEN_EXISTING = 3
Const FILE_ATTRIBUTE_ARCHIVE = &H20
hfile = -1
hfile = CreateFile("\\123.45.67.890\cdrive\windows\explorer.exe", GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hfile = -1 Then
varConnected = False
Else
varConnected = True
End If
***********************************************************
I thought that since I was moving to .Net I would take advantage of using the simpler built in function to check for files.
But where the CreateFile API would return results regardless of connectivity within a couple seconds, the built-in File.Exists takes forever.
Before I go back and modify my code to use the API again, I would appreciate any ideas on what may be wrong, or any better suggestions on how to accomplish my goal.
Thanks.
