C# 使用FTP协议列出包含大约2000000个文件的目录

C# 使用FTP协议列出包含大约2000000个文件的目录,c#,linux,ftp,listdir,C#,Linux,Ftp,Listdir,我在一个FTP服务器上有一个包含大约2000000个文件的目录。此FTP服务器基于Linux。我想列出这个目录中的文件(带有最后修改日期的文件名),但Filezilla和Core FTP Pro都不能这样做,列表操作将失败。我曾尝试使用FTPWebRequest类在C#中编写自己的应用程序并运行ListDirectory方法,但它也无法列出目录中的文件,FTPWebRequest超时。 有没有办法使用任何协议(如FTP、SMB等)甚至通过执行bash脚本来列出目录中这么多文件的名称 在c#中列出

我在一个FTP服务器上有一个包含大约2000000个文件的目录。此FTP服务器基于Linux。我想列出这个目录中的文件(带有最后修改日期的文件名),但Filezilla和Core FTP Pro都不能这样做,列表操作将失败。我曾尝试使用FTPWebRequest类在C#中编写自己的应用程序并运行ListDirectory方法,但它也无法列出目录中的文件,FTPWebRequest超时。
有没有办法使用任何协议(如FTP、SMB等)甚至通过执行bash脚本来列出目录中这么多文件的名称

在c#中列出目录的函数是:

public static List ListFtpDirectory(字符串地址、字符串用户名、字符串密码)
{
List filesName=新列表();
尝试
{
FtpWebRequest=(FtpWebRequest)WebRequest.Create(地址);
request.Method=WebRequestMethods.Ftp.ListDirectory;
request.Credentials=新的网络凭据(用户名、密码);
request.usesponsive=true;
使用(FtpWebResponse响应=(FtpWebResponse)request.GetResponse())
使用(StreamReader=newstreamreader(response.GetResponseStream())
{
文件名=
reader.ReadToEnd().Split(新字符串[]{“\r\n”},StringSplitOptions.RemoveEmptyEntries.ToList();
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
返回文件名;
}
非常感谢您的帮助。

我的C#不流利,但是您没有提供收到的错误的确切文本

以下是应在您的环境中工作的Powershell脚本:

$Server = "ftp://ftp.example.com/"
$User = "anonymous@example.com"
$Pass = "anonymous@anonymous.com"

Function Get-FtpDirectory($Directory) {

    # Credentials
    $FTPRequest = [System.Net.FtpWebRequest]::Create("$($Server)$($Directory)")
    $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($User,$Pass)
    $FTPRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails

    # Don't want Binary, Keep Alive unecessary.
    $FTPRequest.UseBinary = $False
    $FTPRequest.KeepAlive = $False

    $FTPResponse = $FTPRequest.GetResponse()
    $ResponseStream = $FTPResponse.GetResponseStream()

    # Create a nice Array of the detailed directory listing
    $StreamReader = New-Object System.IO.Streamreader $ResponseStream
    $DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine)
    $StreamReader.Close()

    # Remove first two elements ( . and .. ) and last element (\n)
    # and take into account empty directories
    If ($DirListing.Length -gt 3) {
        $DirListing = $DirListing[2..($DirListing.Length-2)]
    }
    else {
        $DirListing = @{}    $DirListing = $DirListing[2..($DirListing.Length-2)] 
    }

    # Close the FTP connection so only one is open at a time
    $FTPResponse.Close()

    # This array will hold the final result
    $FileTree = @()

    # Loop through the listings
    foreach ($CurLine in $DirListing) {

        # Split line into space separated array
        $LineTok = ($CurLine -split '\ +')

        # Get the filename (can even contain spaces)
        $CurFile = $LineTok[8..($LineTok.Length-1)]

        # Figure out if it's a directory. Super hax.
        $DirBool = $LineTok[0].StartsWith("d")

        # Determine what to do next (file or dir?)
        If ($DirBool) {
            # Recursively traverse sub-directories
            $FileTree += ,(Get-FtpDirectory "$($Directory)$($CurFile)/")
        } Else {
            # Add the output to the file tree
            $FileTree += ,"$($Directory)$($CurFile)"
        }
    }

    Return $FileTree

}

请在说明中添加现有代码因为堆栈溢出对您隐藏了关闭原因:寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中复制它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。我已经添加了现有代码…@Nicklaus BrainRead再次:没有明确问题陈述的问题对其他读者没有用处我的问题有这样的问题吗?我试着把它写清楚,并编辑了@Sir RufoThank you@Walkabout Tigger,但这不起作用,它无法检索目录信息。
$Server = "ftp://ftp.example.com/"
$User = "anonymous@example.com"
$Pass = "anonymous@anonymous.com"

Function Get-FtpDirectory($Directory) {

    # Credentials
    $FTPRequest = [System.Net.FtpWebRequest]::Create("$($Server)$($Directory)")
    $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($User,$Pass)
    $FTPRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails

    # Don't want Binary, Keep Alive unecessary.
    $FTPRequest.UseBinary = $False
    $FTPRequest.KeepAlive = $False

    $FTPResponse = $FTPRequest.GetResponse()
    $ResponseStream = $FTPResponse.GetResponseStream()

    # Create a nice Array of the detailed directory listing
    $StreamReader = New-Object System.IO.Streamreader $ResponseStream
    $DirListing = (($StreamReader.ReadToEnd()) -split [Environment]::NewLine)
    $StreamReader.Close()

    # Remove first two elements ( . and .. ) and last element (\n)
    # and take into account empty directories
    If ($DirListing.Length -gt 3) {
        $DirListing = $DirListing[2..($DirListing.Length-2)]
    }
    else {
        $DirListing = @{}    $DirListing = $DirListing[2..($DirListing.Length-2)] 
    }

    # Close the FTP connection so only one is open at a time
    $FTPResponse.Close()

    # This array will hold the final result
    $FileTree = @()

    # Loop through the listings
    foreach ($CurLine in $DirListing) {

        # Split line into space separated array
        $LineTok = ($CurLine -split '\ +')

        # Get the filename (can even contain spaces)
        $CurFile = $LineTok[8..($LineTok.Length-1)]

        # Figure out if it's a directory. Super hax.
        $DirBool = $LineTok[0].StartsWith("d")

        # Determine what to do next (file or dir?)
        If ($DirBool) {
            # Recursively traverse sub-directories
            $FileTree += ,(Get-FtpDirectory "$($Directory)$($CurFile)/")
        } Else {
            # Add the output to the file tree
            $FileTree += ,"$($Directory)$($CurFile)"
        }
    }

    Return $FileTree

}