Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

VictorShumiF1

+1
noxide

Ok cool, thank you, I got around the limitation by doing this:
 

 public static string FtpFilesSecurely(string file, string host, string direction, BackgroundWorker bw)
        {
                            var fileInfo = session.GetFileInfo(file);
                            //CalculateDownload
                            worker1.DoWork += (sender, e) =>
                                e.Result = worker1_DoWork(fileInfo, FTP, bw, file);
                            worker1.RunWorkerAsync();

                            //Download files
                            TransferOperationResult transferResult =
                                session.GetFiles(file, FTP.sourceDirectory, false, transferOptions);

                            // Throw on any error
                            transferResult.Check();

                            // Print results
                            return transferResult.IsSuccess ? "Download Succeeded." : "Download Failed.";
}
public static object worker1_DoWork(RemoteFileInfo fileInfo, CompanyFtpInfo FTP, BackgroundWorker bw, string file)
        {
            const string filePart = ".filepart";
            file = FTP.sourceDirectory + file + filePart;
            while (!File.Exists(file))
            {
                System.Threading.Thread.Sleep(1000);
            }
            CalculateDownload(fileInfo, bw, file);
            return "Calculation Complete.";
        }
private static void CalculateDownload(RemoteFileInfo fileInfo, BackgroundWorker bw, string file)
        {
            long remoteFileSize = fileInfo.Length;
            var localFile = new FileInfo(file);
                long localFileSize = localFile.Length;
                while (localFileSize < remoteFileSize)
                {
                    long progress = localFileSize*100/remoteFileSize;
                    bw.ReportProgress((int) progress);

                    localFile = new FileInfo(file);
                    localFileSize = localFile.Length;
                   
                    //this works because as a file is downloaded it is called filename.ext.filepart
                    //when the file is done downloading it is then called filename.ext so therefore
                    //the original name doesnt exist anymore.
                    if (!File.Exists(file))
                        break;
                }
        }


It doesn't work 100% but works pretty good, an advanced programmer could probably make it work better.
noxide

Sorry I didnt enter any code, but I didnt have any to enter using winscp, read through docs and google but couldnt find a way. I'd like to use winscp to report progress like shown below:
FileStream stream = File.OpenRead(filePath);

                byte[] buffer = new byte[2048];
                int contentLen = stream.Read(buffer, 0, buffer.Length);
                int totalReadBytesCount = contentLen;

                Stream reqStream = request.GetRequestStream();
                while (contentLen != 0)
                {
                    reqStream.Write(buffer, 0, buffer.Length);
                    contentLen = stream.Read(buffer, 0, buffer.Length);
                    totalReadBytesCount += contentLen;
                    var progress = totalReadBytesCount * 100.0 / stream.Length;
                    bw.ReportProgress((int)progress);
                }
[/code]
noxide

report progress

Is there anyway to report progress to a background worker with session.GetFiles?

Thank you.