report progress

Advertisement

Advertisement

noxide
Joined:
Posts:
3

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]

Reply with quote

noxide
Joined:
Posts:
3

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.

Reply with quote

Advertisement

You can post new topics in this forum