public void Download()
        {
            if (Protocol == SSHTransferProtocol.SCP)
            {
                if (!ScpClt.IsConnected)
                {
                    return;
                }
                ScpClt.Download(SrcFile, new FileInfo(DstFile));
            }

            if (Protocol == SSHTransferProtocol.SFTP)
            {
                if (!SftpClt.IsConnected)
                {
                    return;
                }

                stream_download       = new FileStream(DstFile, FileMode.Create);
                async_download_result =
                    (SftpDownloadAsyncResult)SftpClt.BeginDownloadFile(
                        SrcFile, stream_download,
                        asyncCallback);
            }
        }
Exemple #2
0
        [Ignore] // placeholder
        public void SftpDownloadAsyncResultConstructorTest()
        {
            AsyncCallback           asyncCallback = null; // TODO: Initialize to an appropriate value
            object                  state         = null; // TODO: Initialize to an appropriate value
            SftpDownloadAsyncResult target        = new SftpDownloadAsyncResult(asyncCallback, state);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
Exemple #3
0
        public void Update()
        {
            var target = new SftpDownloadAsyncResult(null, null);

            target.Update(123);
            target.Update(431);

            Assert.AreEqual(431UL, target.DownloadedBytes);
        }
Exemple #4
0
        public void SftpDownloadAsyncResultConstructorTest()
        {
            const AsyncCallback asyncCallback = null;
            var state  = new object();
            var target = new SftpDownloadAsyncResult(asyncCallback, state);

            Assert.IsFalse(target.CompletedSynchronously);
            Assert.IsFalse(target.EndInvokeCalled);
            Assert.IsFalse(target.IsCompleted);
            Assert.IsFalse(target.IsDownloadCanceled);
            Assert.AreEqual(0UL, target.DownloadedBytes);
            Assert.AreSame(state, target.AsyncState);
        }
Exemple #5
0
        private void __DOWNLOAD_THREAD()
        {
            Modify_progressBar1(progressBar1, 0);
            Modify_DownloadStatusTextBox(DownloadStatusTextBox, "");

            Modify_DownloadStatusTextBox(DownloadStatusTextBox, "Initializing..." + System.Environment.NewLine);

            try
            {
                using (SftpClient client = new SftpClient(con))
                {
                    client.Connect();
                    Console.WriteLine("Connected to the server!");
                    Modify_DownloadStatusTextBox(DownloadStatusTextBox, "Connected to the server " + this.hostname + "." + System.Environment.NewLine);

                    foreach (string remoteFileName in RemoteFileTextbox.Lines)
                    {
                        DOWNLOAD(client, remoteFileName);
                    }

                    if (sftpAsyncr.IsDownloadCanceled)
                    {
                        Modify_progressBar1(progressBar1, 0);
                        Modify_DownloadLabel(DownloadLabel, "Status: Canceled");
                        Modify_downloadBtnStatus(downloadBtn);
                        sftpAsyncr = null;
                    }
                    else
                    {
                        Modify_progressBar1(progressBar1, 0);
                        Modify_DownloadLabel(DownloadLabel, "Status: Completed");
                        Modify_downloadBtnStatus(downloadBtn);
                        sftpAsyncr = null;
                    }

                    client.Disconnect();
                }
            }
            catch (Exception ex)
            {
                Modify_progressBar1(progressBar1, 0);
                Modify_DownloadLabel(DownloadLabel, "Status: Error occurred");
                Modify_DownloadStatusTextBox(DownloadStatusTextBox, "Error occurred, try again!" + System.Environment.NewLine);
                Modify_downloadBtnStatus(downloadBtn);
                sftpAsyncr = null;
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #6
0
        public void EndInvoke_CompletedWithException()
        {
            object    state     = "STATE";
            Exception exception = new IOException();
            var       target    = new SftpDownloadAsyncResult(null, state);

            target.SetAsCompleted(exception, true);

            try
            {
                target.EndInvoke();
                Assert.Fail();
            }
            catch (IOException ex)
            {
                Assert.AreSame(exception, ex);
            }
        }
Exemple #7
0
        public void SetAsCompleted_Exception_CompletedSynchronously()
        {
            var          downloadCompleted = new ManualResetEvent(false);
            object       state             = "STATE";
            Exception    exception         = new IOException();
            IAsyncResult callbackResult    = null;
            var          target            = new SftpDownloadAsyncResult(asyncResult =>
            {
                downloadCompleted.Set();
                callbackResult = asyncResult;
            }, state);

            target.SetAsCompleted(exception, true);

            Assert.AreSame(target, callbackResult);
            Assert.IsFalse(target.IsDownloadCanceled);
            Assert.IsTrue(target.IsCompleted);
            Assert.IsTrue(target.CompletedSynchronously);
            Assert.IsTrue(downloadCompleted.WaitOne(TimeSpan.Zero));
        }
Exemple #8
0
            protected override async Task PerformIO()
            {
                bool               hadToConnect = _client.ConnectIfNeeded();
                string             fullFilePath = ODFileUtils.CombinePaths(Folder, FileName, '/');
                SftpFileAttributes attribute    = _client.GetAttributes(fullFilePath);

                using (MemoryStream stream = new MemoryStream()) {
                    SftpDownloadAsyncResult res = (SftpDownloadAsyncResult)_client.BeginDownloadFile(fullFilePath, stream);
                    while (!res.AsyncWaitHandle.WaitOne(100))
                    {
                        if (DoCancel)
                        {
                            res.IsDownloadCanceled = true;
                            _client.DisconnectIfNeeded(hadToConnect);
                            return;
                        }
                        OnProgress((double)res.DownloadedBytes / (double)1024 / (double)1024, "?currentVal MB of ?maxVal MB downloaded", (double)attribute.Size / (double)1024 / (double)1024, "");
                    }
                    _client.EndDownloadFile(res);
                    FileContent = stream.ToArray();
                }
                _client.DisconnectIfNeeded(hadToConnect);
                await Task.Run(() => { });                //Gets rid of a compiler warning and does nothing.
            }
Exemple #9
0
        /// <summary>
        /// Download method using SSH.NET SFTP implementation for async files downloading.
        /// </summary>
        private void DOWNLOAD(SftpClient client, String remoteFileName)
        {
            bool IsExists = client.Exists(remoteFileName);

            Modify_DownloadStatusTextBox(DownloadStatusTextBox, System.Environment.NewLine + "Checking if remote file " + remoteFileName + " exists..." + System.Environment.NewLine);

            if (IsExists)
            {
                Console.WriteLine("File exists... continue!");
                Modify_DownloadStatusTextBox(DownloadStatusTextBox, "File exists..." + System.Environment.NewLine);

                SftpFileAttributes att = client.GetAttributes(remoteFileName);
                fileSize = att.Size;
                Modify_DownloadStatusTextBox(DownloadStatusTextBox, "File size is: " + fileSize + "." + System.Environment.NewLine);

                Console.WriteLine("File size is: " + fileSize);

                string name          = remoteFileName;
                int    pos           = name.LastIndexOf("/") + 1;
                String localFileName = DownloadPathTextbox.Text + "/" + name.Substring(pos, name.Length - pos);

                using (FileStream fs = new FileStream(localFileName, FileMode.Create, FileAccess.Write))
                {
                    Console.WriteLine("Begin Async Download!");
                    Modify_DownloadStatusTextBox(DownloadStatusTextBox, "Downloading file " + remoteFileName + " ..." + System.Environment.NewLine);

                    IAsyncResult asyncr = client.BeginDownloadFile(remoteFileName, fs);
                    sftpAsyncr = (SftpDownloadAsyncResult)asyncr;


                    while (!sftpAsyncr.IsCompleted && !sftpAsyncr.IsDownloadCanceled)
                    {
                        int pct = Convert.ToInt32(((double)sftpAsyncr.DownloadedBytes / (double)fileSize) * 100);

                        double temp = (double)sftpAsyncr.DownloadedBytes / (double)fileSize;
                        Console.WriteLine("Downloaded Bytes: " + sftpAsyncr.DownloadedBytes);
                        Console.WriteLine("Downloaded: " + temp);
                        Console.WriteLine("File size is: " + fileSize);
                        Console.WriteLine(pct);
                        Modify_progressBar1(progressBar1, pct);
                        Modify_DownloadLabel(DownloadLabel, "Status: " + pct + " %");
                        Modify_DownloadBytesLabel(DownloadBytesLabel, sftpAsyncr.DownloadedBytes);
                    }
                    client.EndDownloadFile(asyncr);

                    fs.Close();
                }

                if (sftpAsyncr.IsDownloadCanceled)
                {
                    Console.WriteLine("File Download has been canceled!");
                    Modify_DownloadStatusTextBox(DownloadStatusTextBox, "File Download has been canceled!" + System.Environment.NewLine);
                }
                else
                {
                    Console.WriteLine("The file " + remoteFileName + " has been successfully downloaded from the server");
                    Modify_DownloadStatusTextBox(DownloadStatusTextBox, "The file " + remoteFileName + " has been downloaded successfully!" + System.Environment.NewLine);
                }
            }
            else
            {
                MessageBox.Show("The file " + remoteFileName + " does not exists on the server.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }