public ListDirectory ( string path, Action |
||
path | string | The path. |
listCallback | Action |
The list callback. |
Résultat | IEnumerable |
private void PollHandler() { var exchange = new Exchange(_processor.Route); var ipaddress = _processor.UriInformation.GetUriProperty("host"); var username = _processor.UriInformation.GetUriProperty("username"); var password = _processor.UriInformation.GetUriProperty("password"); var destination = _processor.UriInformation.GetUriProperty("destination"); var port = _processor.UriInformation.GetUriProperty<int>("port"); var interval = _processor.UriInformation.GetUriProperty<int>("interval", 100); var secure = _processor.UriInformation.GetUriProperty<bool>("secure"); do { Thread.Sleep(interval); try { if (secure) { SftpClient sftp = null; sftp = new SftpClient(ipaddress, port, username, password); sftp.Connect(); foreach (var ftpfile in sftp.ListDirectory(".")) { var destinationFile = Path.Combine(destination, ftpfile.Name); using (var fs = new FileStream(destinationFile, FileMode.Create)) { var data = sftp.ReadAllText(ftpfile.FullName); exchange.InMessage.Body = data; if (!string.IsNullOrEmpty(data)) { sftp.DownloadFile(ftpfile.FullName, fs); } } } } else { ReadFtp(exchange); } } catch (Exception exception) { var msg = exception.Message; } } while (true); }
/// <summary> /// Lists the files. /// </summary> /// <param name="remotePath">The remote path.</param> /// <param name="failRemoteNotExists">if set to <c>true</c> [fail remote not exists].</param> /// <returns></returns> /// <exception cref="System.Exception"></exception> public List<IRemoteFileInfo> ListFiles(string remotePath) { List<IRemoteFileInfo> fileList = new List<IRemoteFileInfo>(); try { this.Log(String.Format("Connecting to Host: [{0}].", this.hostName), LogLevel.Minimal); using (SftpClient sftp = new SftpClient(this.hostName, this.portNumber, this.userName, this.passWord)) { sftp.Connect(); this.Log(String.Format("Connected to Host: [{0}].", this.hostName), LogLevel.Verbose); if (!sftp.Exists(remotePath)) { this.Log(String.Format("Remote Path Does Not Exist: [{0}].", this.hostName), LogLevel.Verbose); if (this.stopOnFailure) throw new Exception(String.Format("Invalid Path: [{0}]", remotePath)); } else { this.Log(String.Format("Listing Files: [{0}].", remotePath), LogLevel.Minimal); this.Log(String.Format("Getting Attributes: [{0}].", remotePath), LogLevel.Verbose); SftpFile sftpFileInfo = sftp.Get(remotePath); if (sftpFileInfo.IsDirectory) { this.Log(String.Format("Path is a Directory: [{0}].", remotePath), LogLevel.Verbose); IEnumerable<SftpFile> dirList = sftp.ListDirectory(remotePath); foreach (SftpFile sftpFile in dirList) fileList.Add(this.CreateFileInfo(sftpFile)); } else { this.Log(String.Format("Path is a File: [{0}].", remotePath), LogLevel.Verbose); fileList.Add(this.CreateFileInfo(sftpFileInfo)); } } } this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal); } catch (Exception ex) { this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal); this.ThrowException("Unable to List: ", ex); } return fileList; }
public static IEnumerable<SftpFile> GetList(string host, string folder, string userName, string password) { using (var sftp = new SftpClient(host, userName, password)) { sftp.ErrorOccurred += Sftp_ErrorOccurred; sftp.Connect(); var toReturn = sftp.ListDirectory(folder).ToList(); sftp.Disconnect(); return toReturn; } }
/// <summary> /// List the files inside a given directory /// </summary> /// <param name="client">The Sftp client</param> /// <param name="dirPath">The directory path</param> /// <param name="silentDownload">Download the files without listing everything</param> /// <returns>The file list collection</returns> public static IEnumerable <String> ListFiles(this RenciSftpClient client, string dirPath, SftpFilter filter, Boolean silentDownload = false) { List <String> files = new List <String> (); IEnumerable <String> result; var entries = client.ListDirectory(dirPath); if (silentDownload) { Console.WriteLine("Downloading {0} entries...", entries.Count()); } foreach (var entry in entries.Where(x => !x.IsDirectory)) { if (filter.IsUnixFileValid(entry.FullName)) { files.Add(entry.FullName); } } result = files; foreach (var subDirPath in entries.Where(x => x.IsDirectory && filter.IsUnixDirValid(x))) { Console.WriteLine(subDirPath); result = result.Union(ListFiles(client, subDirPath.FullName, filter, silentDownload)); } return(result); }
private void DownloadDirectory(SftpClient client, string source, string destination) { var files = client.ListDirectory(source); foreach (var file in files) { if (!file.IsDirectory && !file.IsSymbolicLink) { DownloadFile(client, file, destination); } else if (file.IsSymbolicLink) { Console.WriteLine("Ignoring symbolic link {0}", file.FullName); } else if (file.Name != "." && file.Name != "..") { var dir = Directory.CreateDirectory(Path.Combine(destination, file.Name)); DownloadDirectory(client, file.FullName, dir.FullName); } } }
public static List<Renci.SshNet.Sftp.SftpFile> GetRemoteFilesOrFoldersList(string host, string user, string password, string remotePath, int type) { List<Renci.SshNet.Sftp.SftpFile> fList = new List<Renci.SshNet.Sftp.SftpFile>(); using (SftpClient client = new SftpClient(host, user, password)) { client.KeepAliveInterval = TimeSpan.FromSeconds(60); client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(180); client.OperationTimeout = TimeSpan.FromMinutes(180); client.Connect(); var fileList = client.ListDirectory(remotePath); foreach (var f in fileList) { if (f.Name != "." && f.Name != "..") { if (type == 1) {// Get folders if (f.IsDirectory) { fList.Add(f); } } else { if (f.IsRegularFile) { fList.Add(f); } } } } } return fList; }
private IEnumerable<SftpFile> GetFilesRecur(SftpClient ssh, SftpFile sftpDir) { if (!sftpDir.IsDirectory) { return new[] {sftpDir}; } var fl = new List<SftpFile>(); foreach (var sftpFile in ssh.ListDirectory(sftpDir.FullName)) { if (sftpFile.IsRegularFile) { fl.Add(sftpFile); } else if (sftpFile.IsDirectory && sftpFile.Name != "." && sftpFile.Name != "..") { fl.AddRange(GetFilesRecur(ssh, sftpFile)); } } return fl; }
private void button1_Click(object sender, EventArgs e) { try { /*if (AllFieldsSet() == false) { Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strPleaseFillAllFields); return; }*/ lvSSHFileBrowser.Items.Clear(); var ssh = new SftpClient(txtHost.Text, int.Parse(this.txtPort.Text), txtUser.Text, txtPassword.Text); ssh.Connect(); var res = ssh.ListDirectory(txtRemoteFolderPath.Text).ToList().OrderBy(file => !file.IsDirectory).ThenBy(file => file.Name); var l = new List<EXImageListViewItem>(); foreach (var item in res) { if (item.Name==".") { continue; } var imglstvitem = new EXImageListViewItem { MyImage = item.IsDirectory ? global::My.Resources.Resources.Folder : global::My.Resources.Resources.File, MyValue = item.FullName, Tag = item }; imglstvitem.SubItems.Add(item.Name); imglstvitem.SubItems.Add(item.IsDirectory ? "" : Tools.Misc.LengthToHumanReadable(item.Length)); l.Add(imglstvitem); } ssh.Disconnect(); lvSSHFileBrowser.Items.AddRange(l.ToArray()); lvSSHFileBrowser.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Walks the sftp site and passes by reference all of the directories and sub directories /// </summary> /// <param name="Files"></param> /// <param name="Path"></param> /// <param name="sftp"></param> /// <param name="ParentDirectory"></param> private static void RemoteDirectories(ref List<RemoteFile> Files, string Path, SftpClient sftp, string ParentDirectory = "") { foreach (SftpFile File in sftp.ListDirectory(Path)) { if (File.Name != "." && File.Name != "..") { RemoteFile Rfile = new RemoteFile(File.Name, File.FullName, File.Length, ParentDirectory,File.IsDirectory,File.LastWriteTime,File.LastWriteTimeUtc); Files.Add(Rfile); if (File.IsDirectory) { string Path2 = Path + "/" + File.Name; RemoteDirectories(ref Files, Path2, sftp, ParentDirectory + "/" + File.Name); } } } }