|
-
Aug 16th, 2001, 08:01 AM
#1
Thread Starter
Junior Member
changing drive mappings
Below is code I have that gets next available drive letter, maps to the drive, copy files into it, and them removes the drive mapping. It works fine the first time you hit the code, but the second time in a session that you do this, you get VB error 68, device unavailabe. When I track the activities in parallel with Explorer, I see on the first go-round, the drive being mapped and then the mapping going away, on the second go-round, the same drive is mapped again but on the statement immediately following the mapping, which is 'chdrive availdriveletter' I get the error 68. Is there some other 'clean up' I need to do other than removing the
mapping for this to work successive times in a vb session. Thanks in advance for your help!
Public Module:
'function to identify next availabe drive letter to be used in map
'of drive letter
Public Function NextAvailableDrive() As String
Dim iDrive As Integer
Dim iFirst As Integer
Dim iFirstFree As Integer, sFirstFree As String, sfirstfreeletter As String
Dim sNextDrive As String
iDrive = 67 'starts looking at D:\
Do
iDrive = iDrive + 1
sNextDrive = Chr$(iDrive) + ":\"
iFirstFree = GetDriveType(sNextDrive)
'Function returns 1 if drive is available
Loop Until iFirstFree = 1
sFirstFree = Chr$(iDrive) + ":\"
sfirstfreeletter = Chr$(iDrive) + ":"
availdrive = sFirstFree
availdriveletter = sfirstfreeletter
End Function
'function to perform the mapping of the the identified next
'availabe drive letter
Public Function MapDrive(LocalDrive As String, _
RemoteDrive As String, Optional Username As String, _
Optional Password As String) As Boolean
Dim NetR As NETCONNECT
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = Left$(LocalDrive, 1) & ":"
NetR.lpRemoteName = RemoteDrive
MapDrive = (WNetAddConnection2(NetR, Username, Password, _
CONNECT_UPDATE_PROFILE) = 0)
End Function
'function to delete the current drive mapping
Public Function RemoveMapDrive(LocalDrive As String) As Boolean
Dim NetR As NETCONNECT
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
RemoveMapDrive = (WNetCancelConnection2(LocalDrive, _
CONNECT_UPDATE_PROFILE, True) = 0)
End Function
Form Code (for when you request files to be copied):
NextAvailableDrive
MapDrive availdriveletter, fileserverdir, "", ""
'change drive to the drive: prompt
ChDrive availdriveletter
'change directory to the mapped root of the drive which
'must be drive:\
'(if you used -chdrive availdrive- it would have gotten you to
'the directory last in use when the drive was previously
'mapped)
ChDir availdrive
MkDir dirname
ChDir dirname
<copy of files to directory>
RemoveMapDrive availdriveletter
Screen.MousePointer = vbDefault
ChDir App.Path
-
Aug 16th, 2001, 08:35 AM
#2
First, I would move the UnMapping code from a function, to the Form Unload event, thus keeping the mapped drive active for the entire duration of the session.
Second, I would set a boolean flag to true the first time the getavailabledrive thing is run, and then if the flag were true, I would run it again. I would just have the code to the copy or the whatever you need to have done.
-
Aug 16th, 2001, 09:03 AM
#3
Thread Starter
Junior Member
I apologize, I left one key critical element out. That the second time you execute the code you will be trying to map to a different directory, so you do need to unmap the drive each time you come thru the code. I do appreciate your feedback!
-
Aug 16th, 2001, 09:17 AM
#4
Ok. How 'bout this?
The variable that you store that next available drive letter in will be empty the first time through. The second time through, however, it will contain the letter that it selected the first time. So, check its contents. If it is not empty, then disconnect that drive right at that point and proceed with getting the next available drive letter. That way it will "appear" to your program that you are doing this for the first time.
-
Aug 16th, 2001, 11:10 AM
#5
Also, might I suggest an alternate way of getting the next available drive letter. Using the GetLogicalDrives API call makes it a snap. If you are interested in a routine that does this, let me know. I have one in my code library that I've had for ever.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|