Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Exists Problem

Status
Not open for further replies.

ViperPit

Programmer
Aug 4, 2005
30
US
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.
smiletiniest.gif
 
I checked out that document, plus did some searching on MSDN, and it appears that the RAPI funcitonality is specific to Windows CE. I am working in an XP and 2K environment, and I looked for rapi.dll in the Windows subfolders of those OSs and found nothing.
Any other suggestions as to why the .Net version of File.Exists takes so long would be greatly appreciated.

Thanks all.
 
I knew that :D Just tought you were playing with windows CE since you are using apis to create files on remote machines ;)

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top