映射前应该先检测哪些可用,再进行映射,以前项目中有用到,建议用WSH对象来进行操作符合编程思想。请参考。
1.检测可用本地驱动符
2.映射网络文件夹到本地驱动盘符
3.解除映射
程序代码:
**************************:Map network drirectory to a local drive
*NetWork
*Source:\\cg12\***\
*Local: X:\
*usr:huangxia
*psw:hx123
Procedure MapNetDRV
Lparameters cLocalAddr,cSourceAddr,cUsr,cPsw
Local oShell As ,nRet
Try
oShell=Createobject('')
oShell.MapNetworkDrive(cLocalAddr,cSourceAddr,.F.,cUsr,cPsw)
Catch To oException&&'WSH'不存在或网络地址不在
Messagebox(oException.Message,0,'Hints',5000)
*/暂不处理
Endtry
*check the directory
If Directory(cLocalAddr)
Return cLocalAddr
Else
Return ''
Endif
Endproc
**
*************************:Unmap network drive
Procedure UnMapNetDRV
Lparameters cDrv
Local oshell As Try
oshell=Createobject('')
oshell.RemoveNetworkDrive(cDrv)
Catch To oException&& WSH error encountered or Directory doesn't exists.
*Messagebox(oException.Message,0,'Hints',5000)
*/暂不处理
Endtry
*check if removed
If !Directory(cDrv)
Return 0
Else
Return -1
Endif
Endproc
**
*************************:Get an available local drive identifier
Procedure AvailableDRV
Declare Integer GetLogicalDriveStrings In kernel32 Intege nBufferLength,String @ lpBuffer
Local nalphabet,cDrv,cDrvFree
cDrv=Replicate(" ",108)&&26*4=104
If GetLogicalDriveStrings(108,@cDrv)#0&&0则出错
nalphabet=65+2&&从'C:'开始
Do While .T.
If !Chr(nalphabet)$cDrv
Clear Dlls 'GetLogicalDriveStrings'
Return Chr(nalphabet)+':'
Endif
nalphabet=nalphabet+1
Enddo
Else
*err
Clear Dlls 'GetLogicalDriveStrings'
Return ""
Endif
Endproc
*////////////////////////////////////////////////////////////////////////////////////////////End of NetWork