简体   繁体   English

如何使用C#和WinSCP从远程目录中删除“。”和“..”文件

[英]How to remove “.” and “..” files from remote directory with C# and WinSCP

I'm trying to get the files count from a remote directory using a SFTP connection, but I'm getting . 我正在尝试使用SFTP连接从远程目录中获取文件数,但我得到了. and .. and these are counted these dots like a files, I have 2 files in the remote directory but is counting 4 files including . ..这些点数像文件一样,我在远程目录中有2个文件,但是计算4个文件,包括. and .. . ..

Someone can help me how to solve this? 有人可以帮我解决这个问题吗?

This is my code: 这是我的代码:

filesCount = session.ListDirectory(DataFile.sRemoteDirectory).Files.Count;                

Thanks! 谢谢!

According to the WinSCP documentation : 根据WinSCP文档

You can use Session.EnumerateRemoteFiles method instead, if you want to: 您可以使用Session.EnumerateRemoteFiles方法,如果您想:

  • List only files matching a wildcard; 仅列出与通配符匹配的文件;
  • List the files recursively; 递归列出文件;
  • Have references to this (.) and parent (..) directories be excluded form the listing. 从列表中排除对此(。)和父(...)目录的引用。

So it appears that you should change your code to do something more like this: 因此,您似乎应该更改代码以执行更多类似的操作:

filesCount = 0; 
filesCount = session.EnumerateRemoteFiles(DataFile.sRemoteDirectory).Files.Count();                
session.Close();

Instead of using ListDirectory you can use EnumerateRemoteFiles and it wont include ".." and "." 您可以使用EnumerateRemoteFiles而不是使用ListDirectory ,它不会包含“..”和“。”。

"." “” and ".." mean this directory and parent directory respectively. 和“..”分别表示该目录和父目录。

The . . and .. are references to this and parent directories respectively on most file systems. ..分别是在大多数文件系统上对this和parent目录的引用。


To filter them, you can use new properties .IsThisDirectory and .IsParentDirectory of the RemoteFileInfo class : 要过滤它们,可以使用RemoteFileInfo类的新属性.IsThisDirectory.IsParentDirectory

filesCount =   
    session.ListDirectory(DataFile.sRemoteDirectory).Files
        .Where(file => !file.IsThisDirectory && !file.IsParentDirectory).Count();

Note that you have to use the Enumerable.Count() extension method , instead of the ICollection.Count property as the result of the Enumerable.Where() is the IEnumerable , not the Collection anymore. 请注意,您必须使用Enumerable.Count()扩展方法 ,而不是ICollection.Count属性,因为Enumerable.Where()的结果是IEnumerable ,而不是Collection


Or to make it even easier, use the Session.EnumerateRemoteFiles() method , which with the EnumerationOptions.None option is functionally equivalent to the Session.ListDirectory() , just that it excludes the . 或者为了使它更容易,使用Session.EnumerateRemoteFiles()方法 ,它与EnumerationOptions.None选项在功能上等同于Session.ListDirectory() ,只是它排除了. and .. . ..

filesCount =   
    session.EnumerateRemoteFiles(
        DataFile.sRemoteDirectory, null, EnumerationOptions.None).Count();               

If you want to filter all directories, use: 如果要筛选所有目录,请使用:

filesCount =   
    session.ListDirectory(DataFile.sRemoteDirectory).Files
        .Where(file => !file.IsDirectory).Count();               

请尝试session.EnumerateRemoteFiles。

Despite its naming, Files collection contains not only files, but all directory entries, including current and parent directory references. 尽管命名,但Files集合不仅包含文件,还包含所有目录条目,包括当前和父目录引用。

If you want to count only files, filter them by IsDirectory property: 如果您只想计算文件,请按IsDirectory属性过滤它们:

var filesCount = session.ListDirectory(dir).Files.Where(x => !x.IsDirectory).Count();

There's also IsParentDirectory and IsThisDirectory properties in the latest versions to filter ".." and "." 在最新版本中还有IsParentDirectoryIsThisDirectory属性来过滤“..”和“。”。 cases without name comparison. 无名称比较的案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM