|
发表于 2020-8-19 23:00:01
|
显示全部楼层
给你一段创建网络连接的代码,创建网络连接后,可以通过网络路径打开服务器的文件
'*************************************************************************
'**函 数 名:ConnectNetDir
'**输 入:szPath(String) - 路径名
'** :szUser(String) - 路径访问用户名
'** :szPwd(String) - 路径访问密码
'**输 出:连接上返回1,连接不上返回0
'**功能描述:连接网络路径,10秒钟内连接不上则返回0
'*************************************************************************
Function ConnectNetDir(szPath As String, szUser As String, szPwd As String)
Dim retVal
Dim loopTime As Integer
Dim nOSName As Long 'OS 名称
Dim nOSVerMajor As Long '操作系统主版本号
Dim nOSVerMinor As Long '操作系统次版本号
Dim nOSVerRevision As Long '操作系统修正号
Dim IsWin98 As Boolean
Dim clsOS As COperateSystem
Set clsOS = New COperateSystem
clsOS.GetOSVersion nOSName, nOSVerMajor, nOSVerMinor, nOSVerRevision
If nOSVerMajor < 5 Then '主版本号>=5,表明是win2000以上的操作系统
IsWin98 = True
Else
IsWin98 = False
End If
Set clsOS = Nothing
'在网络路径名中的机器域名后加一个空格
'即"\\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
'检测网络路径是否能够访问
If Dir(szPath & "\", vbDirectory) = "" Then
loopTime = loopTime + 1
Sleep 1000
Else
ConnectNetDir = 1
Exit Function
End If
Loop
ConnectNetDir = 0
End Function |
|