public async Task <Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default) { if (String.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } EnsureClientConnected(); try { var stream = new MemoryStream(); await Task.Factory.FromAsync(_client.BeginDownloadFile(NormalizePath(path), stream, null, null), _client.EndDownloadFile).AnyContext(); stream.Seek(0, SeekOrigin.Begin); return(stream); } catch (SftpPathNotFoundException ex) { if (_logger.IsEnabled(LogLevel.Trace)) { _logger.LogTrace(ex, "Error trying to get file stream: {Path}", path); } return(null); } }
/// <summary> /// Download the specified file /// </summary> /// <param name="settings">ftp server settings</param> /// <param name="fileName">Name of the file on ftp server</param> /// <param name="targetPath">Path of the target directory on local file system</param> /// <returns></returns> public async Task <bool> DownloadAsync(FTPSettings settings, string fileName, string targetPath) { var directoryInfo = new DirectoryInfo(targetPath); if (!directoryInfo.Exists) { directoryInfo.Create(); } var connectionInfo = CreateConnectionInfo(settings); using (var sftp = new SftpClient(connectionInfo)) { sftp.Connect(); ChangeServerPath(sftp, settings.ServerPath); using (var saveFile = File.OpenWrite(Path.Combine(targetPath, fileName))) { await Task.Factory.FromAsync(sftp.BeginDownloadFile(fileName, saveFile), sftp.EndDownloadFile); } sftp.Disconnect(); } return(true); }
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); } }
async Task DownloadFileAsync(SftpClient client, string source, string destination) { using (var saveFile = System.IO.File.OpenWrite(destination)) { var task = Task.Factory.FromAsync(client.BeginDownloadFile(source, saveFile), client.EndDownloadFile); await task; } }
public IAsyncResult BeginDownloadFile(string path, Stream stream, AsyncCallback callback, Action <ulong> progress) { ColoredConsole.WriteLine(ConsoleColor.Cyan, "SSH.NET: Beginning download of file {0}...", path); var ar = _connection.BeginDownloadFile(path, stream, callback, progress); ColoredConsole.WriteLine(ConsoleColor.Green, "SSH.NET: File {0} is being downloaded.", path); return(ar); }
/// <summary> /// Gets All Registrations file from SFTP /// </summary> /// <param name="sftpServerUrl">SFTP Server URL</param> /// <param name="sftpPath">SFTP's Path</param> /// <param name="userID">SFTP's User Id</param> /// <param name="password">The Password</param> /// <param name="targetPath"> Target path to download the file.</param> /// <returns> file path </returns> public string GetAllXMLfileFromSFTP(string sftpServerUrl, string sftpPath, string userID, string password, string targetPath) { string fileName = null; SftpClient sftpclient; FileStream mem = null; this.syncLogger.Log(System.Diagnostics.TraceEventType.Information, LoggingMessageId.DBSyncPollerGenericMessage, "Begin SftpHelper.GetAllXMLfileFromSFTP"); using (sftpclient = new SftpClient(sftpServerUrl, userID, password)) { // connecting to sftp sftpclient.Connect(); foreach (SftpFile file in sftpclient.ListDirectory(sftpPath)) { fileName = file.Name; if (fileName != null && fileName.IndexOf("ALL") > 0) { break; } } targetPath = targetPath + "F" + DateTime.Now.ToFileTime(); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } // downloading the file using (mem = File.Create(targetPath + "\\" + fileName)) { var asynch = sftpclient.BeginDownloadFile(sftpPath + fileName, mem, null, null); while (!asynch.IsCompleted) { Thread.Sleep(100); } sftpclient.EndDownloadFile(asynch); } // unzip and save the file this.UnzipFile(targetPath + "\\" + fileName, targetPath); sftpclient.Disconnect(); } using (ZipArchive zip = ZipFile.Open(targetPath + "\\" + fileName, ZipArchiveMode.Read)) { foreach (ZipArchiveEntry entry in zip.Entries) { if (entry.Name != null && entry.Name.IndexOf("ALL") > 0) { fileName = entry.Name; } } } this.syncLogger.Log(System.Diagnostics.TraceEventType.Information, LoggingMessageId.DBSyncPollerGenericMessage, "End SftpHelper.GetAllXMLfileFromSFTP"); return(targetPath + "\\" + fileName.Substring(0, fileName.LastIndexOf(".")) + ".xml"); }
public void Test_Sftp_BeginDownloadFile_FileNameIsNull() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); sftp.BeginDownloadFile(null, new MemoryStream(), null, null); sftp.Disconnect(); } }
public void Test_Sftp_BeginDownloadFile_FileNameIsWhiteSpace() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); sftp.BeginDownloadFile(" ", new MemoryStream(), null, null); sftp.Disconnect(); } }
public void Test_Sftp_BeginDownloadFile_StreamIsNull() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); sftp.BeginDownloadFile("aaaa", null, null, null); sftp.Disconnect(); } }
public static Task DownloadAsync(this SftpClient client, string path, Stream output, Action <ulong> downloadCallback, TaskFactory factory = null, TaskCreationOptions creationOptions = default(TaskCreationOptions), TaskScheduler scheduler = null) { return((factory = factory ?? Task.Factory).FromAsync( client.BeginDownloadFile(path, output, null, null, downloadCallback), client.EndDownloadFile, creationOptions, scheduler ?? factory.Scheduler ?? TaskScheduler.Current)); }
static async Task DownloadFileAsync(string source, string destination, SftpClient sftp) { //Создаем директорию, дабы сука ОНО КАЧАЛОСЬ! //если директория есть ниче не произойдет DirectoryInfo di = Directory.CreateDirectory(Path.GetDirectoryName(destination)); using (var saveFile = File.OpenWrite(destination)) { var task = Task.Factory.FromAsync(sftp.BeginDownloadFile(source, saveFile), sftp.EndDownloadFile); await task; } }
public async Task <Stream> GetSettlementFile(string subAccount, string settlement) { using (var sftp = new SftpClient(_connectionInfo)) { sftp.Connect(); var stream = new MemoryStream(); await Task.Factory.FromAsync(sftp.BeginDownloadFile($"/settlements/inbox/xml/{_settings.OrgNo}/{subAccount}/{subAccount}-{settlement}.xml", stream), sftp.EndDownloadFile); stream.Seek(0, SeekOrigin.Begin); return(stream); } }
public void Test_Sftp_EndDownloadFile_Invalid_Async_Handle() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); var filename = Path.GetTempFileName(); this.CreateTestFile(filename, 1); sftp.UploadFile(File.OpenRead(filename), "test123"); var async1 = sftp.BeginListDirectory("/", null, null); var async2 = sftp.BeginDownloadFile("test123", new MemoryStream(), null, null); sftp.EndDownloadFile(async1); } }
private async Task FetchFtpFolderFilesAsync(SftpClient client, DataSourceFetche fetch, Predicate <string> filenameMatcher, string path) { if (IsAlreadyVisited(path)) { return; } client.ChangeDirectory(path); var entries = client.ListDirectory(path); await TaskWhenAllOneAtATime( entries.ConvertAll(async file => { if (file.IsDirectory) { await FetchFtpFolderFilesAsync(client, fetch, filenameMatcher, file.FullName); } else if (file.IsRegularFile) { if (IsAlreadyVisited(file.FullName)) { return; } if (!filenameMatcher(file.Name)) { return; } await FetchTheItemAsync( fetch, new FileDetails(file), DataSourceFetchItem.DataSourceFetchItemTypes.Original, null, async fd => { var fn = Stuff.GetTempFileName(Path.GetExtension(fd.FullName), Runner.TempFolderPath); using (var st = File.Create(fn)) { Trace.WriteLine($"Starting {fd.FullName} to [{fn}]"); await Task.Factory.FromAsync( client.BeginDownloadFile(fd.FullName, st, null, null, amt => Trace.WriteLine($"Downloading {fd.FullName} to [{fn}] => {amt}/{fd.Size}")), client.EndDownloadFile); Trace.WriteLine($"Finishing {fd.FullName} to [{fn}]"); } return(fn); }); } })); }
/// <inheritdoc /> public async Task DownloadFileAsync(Stream fileResult, string fileLocation) { if (string.IsNullOrWhiteSpace(fileLocation)) { throw new ArgumentNullException(paramName: nameof(fileLocation), "File location must not be null."); } try { sftpClient.Connect(); await Task.Run(() => { var r = sftpClient.BeginDownloadFile(fileLocation, fileResult); r.AsyncWaitHandle.WaitOne(); }); } finally { sftpClient.Disconnect(); } }
public static Task DownloadToStreamAsync(this SftpClient client, string filePath, Stream outputStream, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); if (client == null) { throw new ArgumentNullException(nameof(client)); } if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentException("File path must not be null or white space."); } if (outputStream == null) { throw new ArgumentNullException(nameof(outputStream)); } return(Task.Factory.FromAsync( client.BeginDownloadFile(filePath, outputStream, null), result => client.EndDownloadFile(result))); }
public byte[] RetrieveFile(string filePath) { var memoryStream = new MemoryStream(); if (!_client.IsConnected) { throw new Exception("Not connected"); } var result = _client.BeginDownloadFile(filePath, memoryStream); while (!result.IsCompleted) { Thread.Sleep(1000); if (_isCancelling) { throw new RFTransientSystemException(this, "Cancelled when retrieving file {0}", filePath); } } _client.EndDownloadFile(result); return(memoryStream.ToArray()); }
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. }
public static void DownloadFile(string file, string ftpFile) { log.WriteLine("\nAttempting to download " + ftpFile + " to " + file + "... ", false); if (File.Exists(file)) { File.Delete(file); } var outputStream = File.OpenWrite(file); var down = sftp.BeginDownloadFile(ftpFile, outputStream); while (!down.IsCompleted) { ; } sftp.EndDownloadFile(down); outputStream.Close(); log.WriteLine("Sucess!\n", false); }
public void Test_Sftp_Ensure_Async_Delegates_Called_For_BeginFileUpload_BeginFileDownload_BeginListDirectory() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); string remoteFileName = Path.GetRandomFileName(); string localFileName = Path.GetRandomFileName(); bool uploadDelegateCalled = false; bool downloadDelegateCalled = false; bool listDirectoryDelegateCalled = false; IAsyncResult asyncResult; // Test for BeginUploadFile. CreateTestFile(localFileName, 1); using (var fileStream = File.OpenRead(localFileName)) { asyncResult = sftp.BeginUploadFile(fileStream, remoteFileName, delegate(IAsyncResult ar) { sftp.EndUploadFile(ar); uploadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(uploadDelegateCalled, "BeginUploadFile"); // Test for BeginDownloadFile. asyncResult = null; using (var fileStream = File.OpenWrite(localFileName)) { asyncResult = sftp.BeginDownloadFile(remoteFileName, fileStream, delegate(IAsyncResult ar) { sftp.EndDownloadFile(ar); downloadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(downloadDelegateCalled, "BeginDownloadFile"); // Test for BeginListDirectory. asyncResult = null; asyncResult = sftp.BeginListDirectory(sftp.WorkingDirectory, delegate(IAsyncResult ar) { sftp.EndListDirectory(ar); listDirectoryDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } Assert.IsTrue(listDirectoryDelegateCalled, "BeginListDirectory"); } }
public void Test_Sftp_Multiple_Async_Upload_And_Download_10Files_5MB_Each() { var maxFiles = 10; var maxSize = 5; using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); var testInfoList = new Dictionary<string, TestInfo>(); for (int i = 0; i < maxFiles; i++) { var testInfo = new TestInfo(); testInfo.UploadedFileName = Path.GetTempFileName(); testInfo.DownloadedFileName = Path.GetTempFileName(); testInfo.RemoteFileName = Path.GetRandomFileName(); this.CreateTestFile(testInfo.UploadedFileName, maxSize); // Calculate hash value testInfo.UploadedHash = CalculateMD5(testInfo.UploadedFileName); testInfoList.Add(testInfo.RemoteFileName, testInfo); } var uploadWaitHandles = new List<WaitHandle>(); // Start file uploads foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; testInfo.UploadedFile = File.OpenRead(testInfo.UploadedFileName); testInfo.UploadResult = sftp.BeginUploadFile(testInfo.UploadedFile, remoteFile, null, null) as SftpUploadAsyncResult; uploadWaitHandles.Add(testInfo.UploadResult.AsyncWaitHandle); } // Wait for upload to finish bool uploadCompleted = false; while (!uploadCompleted) { // Assume upload completed uploadCompleted = true; foreach (var testInfo in testInfoList.Values) { var sftpResult = testInfo.UploadResult; if (!testInfo.UploadResult.IsCompleted) { uploadCompleted = false; } } Thread.Sleep(500); } // End file uploads foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; sftp.EndUploadFile(testInfo.UploadResult); testInfo.UploadedFile.Dispose(); } // Start file downloads var downloadWaitHandles = new List<WaitHandle>(); foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; testInfo.DownloadedFile = File.OpenWrite(testInfo.DownloadedFileName); testInfo.DownloadResult = sftp.BeginDownloadFile(remoteFile, testInfo.DownloadedFile, null, null) as SftpDownloadAsyncResult; downloadWaitHandles.Add(testInfo.DownloadResult.AsyncWaitHandle); } // Wait for download to finish bool downloadCompleted = false; while (!downloadCompleted) { // Assume download completed downloadCompleted = true; foreach (var testInfo in testInfoList.Values) { var sftpResult = testInfo.DownloadResult; if (!testInfo.DownloadResult.IsCompleted) { downloadCompleted = false; } } Thread.Sleep(500); } var hashMatches = true; var uploadDownloadSizeOk = true; // End file downloads foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; sftp.EndDownloadFile(testInfo.DownloadResult); testInfo.DownloadedFile.Dispose(); testInfo.DownloadedHash = CalculateMD5(testInfo.DownloadedFileName); if (!(testInfo.UploadResult.UploadedBytes > 0 && testInfo.DownloadResult.DownloadedBytes > 0 && testInfo.DownloadResult.DownloadedBytes == testInfo.UploadResult.UploadedBytes)) { uploadDownloadSizeOk = false; } if (!testInfo.DownloadedHash.Equals(testInfo.UploadedHash)) { hashMatches = false; } } // Clean up after test foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; sftp.DeleteFile(remoteFile); File.Delete(testInfo.UploadedFileName); File.Delete(testInfo.DownloadedFileName); } sftp.Disconnect(); Assert.IsTrue(hashMatches, "Hash does not match"); Assert.IsTrue(uploadDownloadSizeOk, "Uploaded and downloaded bytes does not match"); } }
async Task IFtpSession.DownloadAsync(string remotePath, string localPath, bool overwrite, bool recursive, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(remotePath)) { throw new ArgumentNullException(nameof(remotePath)); } if (string.IsNullOrWhiteSpace(localPath)) { throw new ArgumentNullException(nameof(localPath)); } FtpObjectType objectType = ((IFtpSession)this).GetObjectType(remotePath); if (objectType == FtpObjectType.Directory) { IEnumerable <Tuple <string, string> > listing = await GetRemoteListingAsync(remotePath, localPath, recursive, cancellationToken); foreach (Tuple <string, string> file in listing) { if (File.Exists(file.Item1) && !overwrite) { continue; } cancellationToken.ThrowIfCancellationRequested(); string directoryPath = Path.GetDirectoryName(file.Item1); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } using (Stream fileStream = File.OpenWrite(file.Item1)) { await Task.Factory.FromAsync(_sftpClient.BeginDownloadFile(file.Item2, fileStream), _sftpClient.EndDownloadFile); } } } else { if (objectType == FtpObjectType.File) { if (File.Exists(localPath)) { if (overwrite) { File.Delete(localPath); } else { throw new IOException(Resources.FileExistsException); } } using (Stream fileStream = File.OpenWrite(localPath)) { await Task.Factory.FromAsync(_sftpClient.BeginDownloadFile(remotePath, fileStream), _sftpClient.EndDownloadFile); } } else { throw new NotImplementedException(Resources.UnsupportedObjectTypeException); } } }
/// <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); } }
public void BeginDownloadFileTest() { ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value string path = string.Empty; // TODO: Initialize to an appropriate value Stream output = null; // TODO: Initialize to an appropriate value AsyncCallback asyncCallback = null; // TODO: Initialize to an appropriate value object state = null; // TODO: Initialize to an appropriate value Action<ulong> downloadCallback = null; // TODO: Initialize to an appropriate value IAsyncResult expected = null; // TODO: Initialize to an appropriate value IAsyncResult actual; actual = target.BeginDownloadFile(path, output, asyncCallback, state, downloadCallback); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
public void Test_Sftp_Multiple_Async_Upload_And_Download_10Files_5MB_Each() { var maxFiles = 10; var maxSize = 5; RemoveAllFiles(); using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); var testInfoList = new Dictionary <string, TestInfo>(); for (int i = 0; i < maxFiles; i++) { var testInfo = new TestInfo(); testInfo.UploadedFileName = Path.GetTempFileName(); testInfo.DownloadedFileName = Path.GetTempFileName(); testInfo.RemoteFileName = Path.GetRandomFileName(); this.CreateTestFile(testInfo.UploadedFileName, maxSize); // Calculate hash value testInfo.UploadedHash = CalculateMD5(testInfo.UploadedFileName); testInfoList.Add(testInfo.RemoteFileName, testInfo); } var uploadWaitHandles = new List <WaitHandle>(); // Start file uploads foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; testInfo.UploadedFile = File.OpenRead(testInfo.UploadedFileName); testInfo.UploadResult = sftp.BeginUploadFile(testInfo.UploadedFile, remoteFile, null, null) as SftpUploadAsyncResult; uploadWaitHandles.Add(testInfo.UploadResult.AsyncWaitHandle); } // Wait for upload to finish bool uploadCompleted = false; while (!uploadCompleted) { // Assume upload completed uploadCompleted = true; foreach (var testInfo in testInfoList.Values) { var sftpResult = testInfo.UploadResult; if (!testInfo.UploadResult.IsCompleted) { uploadCompleted = false; } } Thread.Sleep(500); } // End file uploads foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; sftp.EndUploadFile(testInfo.UploadResult); testInfo.UploadedFile.Dispose(); } // Start file downloads var downloadWaitHandles = new List <WaitHandle>(); foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; testInfo.DownloadedFile = File.OpenWrite(testInfo.DownloadedFileName); testInfo.DownloadResult = sftp.BeginDownloadFile(remoteFile, testInfo.DownloadedFile, null, null) as SftpDownloadAsyncResult; downloadWaitHandles.Add(testInfo.DownloadResult.AsyncWaitHandle); } // Wait for download to finish bool downloadCompleted = false; while (!downloadCompleted) { // Assume download completed downloadCompleted = true; foreach (var testInfo in testInfoList.Values) { var sftpResult = testInfo.DownloadResult; if (!testInfo.DownloadResult.IsCompleted) { downloadCompleted = false; } } Thread.Sleep(500); } var hashMatches = true; var uploadDownloadSizeOk = true; // End file downloads foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; sftp.EndDownloadFile(testInfo.DownloadResult); testInfo.DownloadedFile.Dispose(); testInfo.DownloadedHash = CalculateMD5(testInfo.DownloadedFileName); if (!(testInfo.UploadResult.UploadedBytes > 0 && testInfo.DownloadResult.DownloadedBytes > 0 && testInfo.DownloadResult.DownloadedBytes == testInfo.UploadResult.UploadedBytes)) { uploadDownloadSizeOk = false; } if (!testInfo.DownloadedHash.Equals(testInfo.UploadedHash)) { hashMatches = false; } } // Clean up after test foreach (var remoteFile in testInfoList.Keys) { var testInfo = testInfoList[remoteFile]; sftp.DeleteFile(remoteFile); File.Delete(testInfo.UploadedFileName); File.Delete(testInfo.DownloadedFileName); } sftp.Disconnect(); Assert.IsTrue(hashMatches, "Hash does not match"); Assert.IsTrue(uploadDownloadSizeOk, "Uploaded and downloaded bytes does not match"); } }
public void Test_Sftp_Ensure_Async_Delegates_Called_For_BeginFileUpload_BeginFileDownload_BeginListDirectory() { RemoveAllFiles(); using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); string remoteFileName = Path.GetRandomFileName(); string localFileName = Path.GetRandomFileName(); bool uploadDelegateCalled = false; bool downloadDelegateCalled = false; bool listDirectoryDelegateCalled = false; IAsyncResult asyncResult; // Test for BeginUploadFile. CreateTestFile(localFileName, 1); using (var fileStream = File.OpenRead(localFileName)) { asyncResult = sftp.BeginUploadFile(fileStream, remoteFileName, delegate(IAsyncResult ar) { sftp.EndUploadFile(ar); uploadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(uploadDelegateCalled, "BeginUploadFile"); // Test for BeginDownloadFile. asyncResult = null; using (var fileStream = File.OpenWrite(localFileName)) { asyncResult = sftp.BeginDownloadFile(remoteFileName, fileStream, delegate(IAsyncResult ar) { sftp.EndDownloadFile(ar); downloadDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } } File.Delete(localFileName); Assert.IsTrue(downloadDelegateCalled, "BeginDownloadFile"); // Test for BeginListDirectory. asyncResult = null; asyncResult = sftp.BeginListDirectory(sftp.WorkingDirectory, delegate(IAsyncResult ar) { sftp.EndListDirectory(ar); listDirectoryDelegateCalled = true; }, null); while (!asyncResult.IsCompleted) { Thread.Sleep(500); } Assert.IsTrue(listDirectoryDelegateCalled, "BeginListDirectory"); } }
private void DownloadFilesFromServer() { try { var filelist = new Dictionary<SftpFile, string>(); var remfold = txtRemoteFolderPath.Text.EndsWith("/") ? txtRemoteFolderPath.Text : txtRemoteFolderPath.Text += "/"; var ssh = new SftpClient(txtHost.Text, int.Parse(txtPort.Text), txtUser.Text, txtPassword.Text); ssh.Connect(); foreach (var i in lvSSHFileBrowser.SelectedItems.Cast<EXImageListViewItem>().Select(item => item.Tag as SftpFile)) { if (i.IsRegularFile) { filelist.Add(i, Path.Combine(txtLocalBrowserPath.Text, i.Name)); } if (i.IsDirectory) { foreach (var file in GetFilesRecur(ssh, i)) { var i1 = file.FullName.Replace(remfold, ""); var i2 = i1.Replace('/', '\\'); var i3 = Path.Combine(txtLocalBrowserPath.Text, i2); filelist.Add(file, i3); } } } long totalsize = filelist.Sum(pair => pair.Key.Length); var result = MessageBox.Show(string.Format(Language.SSHTransfer_DownloadFilesFromServer_Download__0__in__1__files_ + "\r\n" + Language.SSHTransfer_DownloadFilesFromServer_Destination_directory__2_, Tools.Misc.LengthToHumanReadable(totalsize), filelist.Count, txtLocalBrowserPath.Text), "Downloading", MessageBoxButtons.YesNoCancel); if (result!=DialogResult.Yes) { EnableButtons(); return; } long totaluploaded = 0; ThreadPool.QueueUserWorkItem(state => { foreach (var file in filelist) { if (!Directory.Exists(Path.GetDirectoryName(file.Value))) { Tools.Misc.ebfFolderCreate(Path.GetDirectoryName(file.Value)); } var asynch = ssh.BeginDownloadFile(file.Key.FullName, new FileStream(file.Value, FileMode.Create)); var sftpAsynch = asynch as SftpDownloadAsyncResult; while (!sftpAsynch.IsCompleted) { SetProgressStatus(totaluploaded + (long)sftpAsynch.DownloadedBytes, totalsize); } totaluploaded += file.Key.Length; ssh.EndDownloadFile(asynch); } EnableButtons(); ssh.Disconnect(); MessageBox.Show(Language.SSHTransfer_DownloadFilesFromServer_Download_finished); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } }