/// <summary> /// 上传文件 /// </summary> /// <param name="ftpAddress"></param> /// <param name="remoteFilename"></param> /// <param name="localFilename"></param> /// <param name="resumeOperation"></param> public bool UploadFile(string ftpAddress, string localFilename, string remoteFilename, bool resumeOperation) { FtpSiteData siteData = ParseFtpAddress(ftpAddress); if (siteData == null) { throw new ArgumentException("Invalid ftp address format!"); } using (FtpConnection connection = new FtpConnection(siteData.Host, siteData.Port, siteData.UserName, siteData.Password)) { SetConnection(connection); AbstractFolder remoteFolder = new FtpFolder(connection); AbstractFile remoteFile = remoteFolder.GetFile(remoteFilename); AbstractFile localFile = new DiskFile(localFilename); if (!resumeOperation || !remoteFile.Exists || remoteFile.Size > localFile.Size) { localFile.CopyTo(remoteFile, true); } else if (remoteFile.Size == localFile.Size) { return(true); } else if (remoteFile.Size < localFile.Size) { byte[] buf = new byte[1024]; int cnt = -1; using (System.IO.Stream remoteStream = remoteFile.OpenWrite(false)) { using (System.IO.Stream localStream = localFile.OpenRead()) { localStream.Seek(remoteFile.Size, System.IO.SeekOrigin.Begin); // can't seek. OpenWrite如果不overwrite自动append //remoteStream.Seek(0, System.IO.SeekOrigin.End); do { cnt = localStream.Read(buf, 0, buf.Length); remoteStream.Write(buf, 0, cnt); } while (cnt == buf.Length); } } } return(true); } //FtpClient client = LoginFtp(ftpAddress); //client.SendFile(localFilename, remoteFilename); }