Closing A FileSystemObject And Mapped Network Drive

See the below code that I have written.

I have created a Mapped Network Drive, then checked a specified file for the Date it was created and the Size using FileSystemObject.

The problem I have is that I cannot Disconnect the network drive using "Net Use strDrive /delete"

I think it is because the FileSystemObject that I have opened still has a hold on that drive as when I run "Net use E: /delete" in DOS it says....

"The device is being accessed by an active process"

The basic code I have is

'CREATES THE MAPPED DRIVE
Shell "net use F: \\CRAIGDESKTOP\C$"

'CHECKS THAT THE FILE EXISTS
If Dir(F:\Program Files\Bank.exe) <> "" Then
           flxVersion.TextMatrix(flxVersion.Row, 1) = MyDesktop

           'OPEN FILE TO GET DETAILS
           Set fso = CreateObject("Scripting.FileSystemObject")
           Set f = fso.GetFile(F:\Program Files\Bank.exe)

           'PUT DETAILS INTO FLEXGRID
           flxVersion.TextMatrix(flxVersion.Row, 2) = f.Size
           flxVersion.TextMatrix(flxVersion.Row, 3) = f.DateCreated
               
           'SET TO NOTHING ???????? DOESNT WORK?????
           Set f = Nothing
           Set fso = Nothing
Else
          flxVersion.TextMatrix(flxVersion.Row, 1) = MyDesktop
          flxVersion.TextMatrix(flxVersion.Row, 2) = "n/a"
          flxVersion.TextMatrix(flxVersion.Row, 2) = "n/a"
End If

'DELETE/DISCONNECT NETWORK DRIVE ??????????
Shell "net use F: /d"


Thanks in advance for any help
CraigBroadmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
Why map a drive,just try to access the file directly using the UNC path:

           'OPEN FILE TO GET DETAILS
           Set fso = CreateObject("Scripting.FileSystemObject")
           Set f = fso.GetFile("\\CRAIGDESKTOP\C$\Program Files\Bank.exe")
0
vinnyd79Commented:
and here:

If Dir("\\CRAIGDESKTOP\C$\Program Files\Bank.exe") <> "" Then
0
vinnyd79Commented:
Or you might want to try shelling the command interpretor with the /c switch instead of shelling net use directly.

i.e.
'CREATES THE MAPPED DRIVE
Shell Environ("ComSpec") & " /c net use F: \\CRAIGDESKTOP\C$", vbHide
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

CraigBroadmanAuthor Commented:
I have to Map A Drive because there is other things that I need to do apart from this.
0
vinnyd79Commented:
I don't think FSO is the problem as you are setting it to Nothing. I think it is the way you are mapping the drive.  Here is a good example of mapping with FSO:

http://www.computerperformance.co.uk/ezine/ezine3.htm
0
CraigBroadmanAuthor Commented:
I can disconnect the drive if i take the fso code out. But i will have a look at the link and see what i can do.

if it works will accept your answer

Thanks
0
PaulHewsCommented:
Here's an API version.  The real advantage here is that you can force disconnection.  It isn't just the FSO, any function call to check on a file seems to cause this problem, even though there are no files being opened.  Sorry I took out your flex and fso code for testing.  Should be easy for you to add back in.

Option Explicit
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long

Const RESOURCETYPE_DISK = &H1
Const ForceDisconnect As Long = 1

Private Type NETRESOURCE
  dwScope As Long
  dwType As Long
  dwDisplayType As Long
  dwUsage As Long
  lpLocalName As String
  lpRemoteName As String
  lpComment As String
  lpProvider As String
End Type


Private Sub Command1_Click()
    Const FileName = "G:\temp\test.txt"
    Dim NetRsc As NETRESOURCE
    NetRsc.lpRemoteName = "\\HOME\C$"
    NetRsc.lpLocalName = "G:"
    NetRsc.dwType = RESOURCETYPE_DISK
   
   
    WNetAddConnection2 NetRsc, vbNullString, vbNullString, 0

    If Dir(FileName) <> "" Then
            Debug.Print FileDateTime(FileName)
            Debug.Print FileLen(FileName)
    End If

   
    If WNetCancelConnection2(NetRsc.lpLocalName, 0, ForceDisconnect) = 0 Then
        Debug.Print "successfully disconnected"
    End If

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CraigBroadmanAuthor Commented:
Thanks very much for your help
0
PaulHewsCommented:
No problem, thanks for the points.
0
CraigBroadmanAuthor Commented:
Have just posted another question. Basically looking for other methods of moving a file, other than FSO.

When I am mapping a client machine I copy their current file to a safe folder so that I can copy an updated version into the orginal location.

However, this safe file cannot be deleted by the client as it says another person or program is currently using it.

If you have any ideas for this i would be grateful, another 250 points up for grabs
0
PaulHewsCommented:
I'll subscribe there, but it looks like the other experts are already helping you out.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.