|
Give you a piece of code to create a network connection. After creating a network connection, you can open the server's file through the network path
'************************************************* ************************
'**Number of letters Name: ConnectNetDir
'**Input: szPath(String)-path name
'**: szUser(String)-path access user name
'**: szPwd(String)-path access password
'** Output: return 1 when connected, and return 0 when not connected
'**Function description: Connect to the network path, return 0 if it fails to connect within 10 seconds
'************************************************* ************************
Function ConnectNetDir(szPath As String, szUser As String, szPwd As String)
Dim retVal
Dim loopTime As Integer
Dim nOSName As Long'OS name
Dim nOSVerMajor As Long'Operating system major version number
Dim nOSVerMinor As Long'Operating system minor version number
Dim nOSVerRevision As Long'Operating system revision number
Dim IsWin98 As Boolean
Dim clsOS As COperateSystem
Set clsOS = New COperateSystem
clsOS.GetOSVersion nOSName, nOSVerMajor, nOSVerMinor, nOSVerRevision
If nOSVerMajor <5 Then'Major version number>=5, it indicates that it is an operating system above win2000
IsWin98 = True
Else
IsWin98 = False
End If
Set clsOS = Nothing
'Add a space after the machine domain name in the network path name
'That is "\\Machine\ShareName"---->"\\Machine\ShareName"
Dim iPos As Integer
iPos = InStr(3, szPath, "\")
szPath = Mid(szPath, 1, iPos-1)&""&Mid(szPath, iPos)
If IsWin98 Then
retVal = Shell("command.com /c net use "&szPath&" "&_
szPwd&"/user:"&szUser, vbHide)
Else
retVal = Shell("cmd.exe /c net use "&Chr(34)&szPath&Chr(34)&_
""&szPwd&"/user:"&szUser, vbHide)
End If
If retVal = 0 Then
ConnectNetDir = 0
Exit Function
End If
On Error Resume Next
loopTime = 0
Do While loopTime <10
'Detect whether the network path can be accessed
If Dir(szPath&"\", vbDirectory) = "" Then
loopTime = loopTime + 1
Sleep 1000
Else
ConnectNetDir = 1
Exit Function
End If
Loop
ConnectNetDir = 0
End Function |
|