思路:通过影射网络驱动器,把路径影射到本地,然后就可以如同本地文件加一样进行处理。
核心代吗如下:
Public Const MAX_FILENAME_LEN = 256
' File and Disk functions.
Public Const DRIVE_CDROM = 5
Public Const DRIVE_FIXED = 3
Public Const DRIVE_RAMDISK = 6
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_UNKNOWN = 0    'Unknown, or unable to be determined.
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
'映射一个网洛驱动器,并断开
Public Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Public Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) As Long
Public inputstr As StringSub Main()
on error goto resume next
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
If App.PrevInstance Then
   End  Exit Sub
End IfIf GetDriveType("d:\") = 5 Then
    panfu = "e"
  
 
ElseIf GetDriveType("e:\") = 5 Then
   
   panfu = "f"
 
 
ElseIf GetDriveType("f:\") = 5 Then
  
  panfu = "g" 
ElseIf GetDriveType("g:\") = 5 Then
 
 panfu = "h"  
 ElseIf GetDriveType("g:\") = 5 Then
 
 panfu = "h" ElseIf GetDriveType("h:\") = 5 Then panfu = "j"
 
 ElseIf GetDriveType("i:\") = 5 Then
 
 panfu = "j"
 
 ElseIf GetDriveType("j:\") = 5 Then
 
 panfu = "k"
  
 ElseIf GetDriveType("k:\") = 5 Then panfu = "l"
 
 ElseIf GetDriveType("l:\") = 5 Then panfu = "m"
 
ElseIf GetDriveType("m:\") = 5 Then
 
 panfu = "n"
  
 ElseIf GetDriveType("n:\") = 5 Then
 
 panfu = "o"
  
 ElseIf GetDriveType("o:\") = 5 Then panfu = "p"
  
 ElseIf GetDriveType("p:\") = 5 Then
 
 panfu = "q"
  
 ElseIf GetDriveType("q:\") = 5 Then panfu = "r"
  
 ElseIf GetDriveType("r:\") = 5 Then
 
 panfu = "s"
 
 ElseIf GetDriveType("s:\") = 5 Then
 
 panfu = "t"
 
End If
call duankai'调用断开
call lianjie '调用连接
End Sub
Sub duankai()   '断开网络连接
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
Dim bb As Long
strLocalDriveLetter = panfu & 
strPassword =""    
    Dim a
   
     If WNetCancelConnection(strLocalDriveLetter, 1) = 0 Then
       Exit Sub  '成功断开
     end
End Subsub lianjie() ‘连接网络
On Error GoTo aaa
    strLocalDriveLetter = panfu & 
    strPassword = ''
    inputstr = "\\ZHEN\temp"  '机器名为zhen得共享目录temp
   strNetworkPathName = inputstr 'path to network drive
    If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
        MsgBox ("网络连接错误")
        Exit Sub
    Else
         frmmain.Show
        'MsgBox ("已经成功映射连接上服务器!")
       
    End If
    
On Error GoTo 0
end sub