public void Test_Sftp_Call_EndListDirectory_Twice() { using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) { sftp.Connect(); var ar = sftp.BeginListDirectory("/", null, null); var result = sftp.EndListDirectory(ar); var result1 = sftp.EndListDirectory(ar); } }
/// <summary> /// Get all file names, inclusive path, of the directory on ftp server /// </summary> /// <param name="settings">ftp server settings</param> /// <param name="recursive">Enumerate sub directories</param> /// <returns></returns> public async Task <IEnumerable <string> > GetFileNamesAsync(FTPSettings settings, bool recursive) { var connectionInfo = CreateConnectionInfo(settings); var fileNames = new List <string>(); var directoryNames = new List <string>(); using (var sftp = new SftpClient(connectionInfo)) { sftp.Connect(); await Task.Factory.FromAsync(sftp.BeginListDirectory(GetServerPath(settings.ServerPath), null, null), (IAsyncResult result) => { var files = sftp.EndListDirectory(result); foreach (var file in files) { if (file.IsDirectory) { if (file.Name == "." || file.Name == ".." || !recursive) { continue; } directoryNames.Add(file.Name); } else { fileNames.Add(file.FullName); } } }); sftp.Disconnect(); } if (recursive && directoryNames.Count > 0) { var childSettings = new FTPSettings { ServerUrl = settings.ServerUrl, ServerPort = settings.ServerPort, Password = settings.Password, Username = settings.Username }; foreach (var directoryName in directoryNames) { childSettings.ServerPath = String.Join("/", settings.ServerPath, directoryName); fileNames.AddRange(await GetFileNamesAsync(childSettings, true)); } } return(fileNames); }
/// <summary> /// List files in the given <paramref name="path"/> asynchronously. /// </summary> /// <param name="client">The SFTP client to use to list files.</param> /// <param name="path">The path to list files from.</param> /// <param name="cancellationToken">Cancellation token </param> /// <returns></returns> public static Task <IEnumerable <SftpFile> > ListDirectoryAsync(this SftpClient client, string path, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException("Path must not be null or white space."); } cancellationToken.ThrowIfCancellationRequested(); return(Task.Factory.FromAsync( client.BeginListDirectory(path, null, null), result => client.EndListDirectory(result))); }
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_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"); } }
public void EndListDirectoryTest() { ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value IAsyncResult asyncResult = null; // TODO: Initialize to an appropriate value IEnumerable<SftpFile> expected = null; // TODO: Initialize to an appropriate value IEnumerable<SftpFile> actual; actual = target.EndListDirectory(asyncResult); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }