如何将局域网内某台计算机上的文件复制到局域网另一台计算机上?
如何将局域网内某台计算机上指定文件夹下的文件复制到局域网另一台计算机上呢?大虾们游过请留声:)
--------------------编程问答-------------------- 用ftp控制吧,或者目标电脑上建共享文件夹 --------------------编程问答--------------------
不了解FTP的原理,大概是个什么思路呢?是不是较麻烦的 --------------------编程问答-------------------- Declare Function CopyFileEx Lib "kernel32.dll" Alias "CopyFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As Long, lpData As Any, ByRef pbCancel As Long, ByVal dwCopyFlags As Long) As Long
Declare Function MoveFileWithProgress Lib "kernel32.dll" Alias "MoveFileWithProgressA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByRef lpProgressRoutine As Long, lpData As Any, ByVal dwFlags As Long) As Long
or...
FSO
or...
SHFileOperation --------------------编程问答-------------------- 用VB的FTP函数比较好点 也效率一点
'模块一
Public bActiveSession As Boolean '用以标示已经建立了FTP连接
Public hOpen As Long '用InternetOpen()函数建立Internet对话后返回的句柄
Public hConnection As Long '用InternetConnect()函数建立FTP连接后返回的句柄
Public dwType As Long
Public EnumItemNameBag As New Collection '用以存放查找到的FTP目录下的文件名
Public EnumItemAttributeBag As New Collection '用以存放与EnumItemNameBag中相对应的文件名的属性
Public Function OPEN_FTP() As Boolean ' 连接FTP
Dim nFlag As Long
On Error GoTo ErrHand
bActiveSession = False
hOpen = 0
hConnection = 0
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PROXY, SERVER_IP, vbNullString, 0)
If hOpen = 0 Then
OPEN_FTP = False
Exit Function
Else
dwType = FTP_TRANSFER_TYPE_BINARY
End If
If Not bActiveSession And hOpen <> 0 Then
nFlag = INTERNET_FLAG_PASSIVE
PORT = Val(SERVER_PORT)
hConnection = InternetConnect(hOpen, SERVER_IP, PORT, STRUser, STRPass, INTERNET_SERVICE_FTP, nFlag, 0)
If hConnection = 0 Then
bActiveSession = False
OPEN_FTP = False
Else
bActiveSession = True
OPEN_FTP = True
End If
End If
Exit Function
ErrHand:
OPEN_FTP = False
End Function
Public Function CLOSE_ftp() As Boolean '关闭FTP
If hConnection <> 0 Then
InternetCloseHandle hConnection
End If
hConnection = 0
bActiveSession = False
End Function
Public Function Look_FTPpath(ByVal FTP_pathname As String) As Integer
Dim Ftp_state As Boolean
On Error GoTo ErrHand
If bActiveSession Then
If FTP_pathname <> "" Then
Ftp_state = FtpSetCurrentDirectory(hConnection, FTP_pathname)
If Ftp_state Then
Look_FTPpath = 0
Else
Look_FTPpath = 1
End If
Else
Look_FTPpath = 2
End If
Else
Look_FTPpath = 3
End If
Exit Function
ErrHand:
Look_FTPpath = 4
End Function
Public Function MD_FTPpath(ByVal FTP_pathname As String) As Integer
Dim Ftp_state As Boolean
On Error GoTo ErrHand
If bActiveSession Then
If FTP_pathname <> "" Then
Ftp_state = FtpCreateDirectory(hConnection, FTP_pathname)
If Ftp_state Then
MD_FTPpath = 0
Else
MD_FTPpath = 1
End If
Else
MD_FTPpath = 2
End If
Else
MD_FTPpath = 3
End If
Exit Function
ErrHand:
MD_FTPpath = 4
End Function
Public Function UP_FTPfile(ByVal LOCAL_filename As String, ByVal FTP_pathname As String, ByVal FTP_filename As String) As Integer
Dim Ftp_state As Boolean
Dim szFileRemote As String, szFileLocal As String
On Error GoTo ErrHand
If bActiveSession Then
szFileLocal = LOCAL_filename
szFileRemote = "\" + FTP_pathname + "\" + FTP_filename
Ftp_state = FtpPutFile(hConnection, szFileLocal, szFileRemote, dwType, 0)
If Ftp_state Then
UP_FTPfile = 0
Else
UP_FTPfile = 1
End If
Else
UP_FTPfile = 2
End If
Exit Function
ErrHand:
UP_FTPfile = 4
End Function
--------------------编程问答-------------------- http://www.m5home.com/bak_blog/article/169.html
'模块2
Option Explicit
Public Const MAX_PATH = 260
Public Const NO_ERROR = 0
Public Const FILE_ATTRIBUTE_READONLY = &H1 '只读文件
Public Const FILE_ATTRIBUTE_HIDDEN = &H2 '隐含文件
Public Const FILE_ATTRIBUTE_SYSTEM = &H4 '系统文件
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10 '目录文件(所获取的文件类型是目录)
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20 '存档文件
Public Const FILE_ATTRIBUTE_NORMAL = &H80 '文件没有的其他属性值
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100 '临时文件
Public Const FILE_ATTRIBUTE_COMPRESSED = &H800 '被压缩的文件和目录
Public Const FILE_ATTRIBUTE_OFFLINE = &H1000
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const scUserAgent = "FTP CLIENT"
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3
Public Const INTERNET_INVALID_PORT_NUMBER = 0
Public Const FTP_TRANSFER_TYPE_ASCII = &H1
Public Const FTP_TRANSFER_TYPE_BINARY = &H1
Public Const INTERNET_FLAG_PASSIVE = &H8000000
Public Const ERROR_INTERNET_EXTENDED_ERROR = 12003
Public Const INTERNET_DEFAULT_FTP_PORT = 21
Public Const INTERNET_DEFAULT_GOPHER_PORT = 70
Public Const INTERNET_DEFAULT_HTTP_PORT = 80
Public Const INTERNET_SERVICE_FTP = 1
Public Const INTERNET_SERVICE_GOPHER = 2
Public Const INTERNET_SERVICE_HTTP = 3
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type WIN32_FIND_DATA
dwFileAttributes As Long '文件的属性
ftCreationTime As FILETIME '文件的创建时间
ftLastAccessTime As FILETIME '文件的最后一次被读写的时间
ftLastWriteTime As FILETIME '文件的最后一次修改的时间
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH '用于存放文件名的字符串
cAlternate As String * 14
End Type
Public Const ERROR_NO_MORE_FILES = 18
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Public Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Boolean
Public Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Public Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" (ByVal hFtpSession As Long, ByVal sFileName As String, ByVal lAccess As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Public Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
给你一个局域网文件传输的例子 --------------------编程问答-------------------- 局域网还要搞这么复杂干嘛呢?
先共享文件夹,然后直接地址访问啊, 就和本地拷贝一样. --------------------编程问答-------------------- 遇到相同问题,VB编的软件要求能自动定时备份到另一台PC。 --------------------编程问答-------------------- net use Y: \\192.168.1.101\c$ password1
net use Z: \\192.168.1.102\c$ password2
copy /y Y:\mydir\test.txt Z:\mydir\test.txt
net use Y: /DELETE
net use Z: /DELETE
补充:VB , 基础类