C#和Powershell,从FTP下载文件[关闭]

问题描述 投票:-2回答:1

我想在ftp服务器上下载文件夹的内容。使用此Powershell脚本,我做到了:

#FTP Server Information - SET VARIABLES
    $ftp = "ftp://my.server.ftp" 
    $user = 'ftp_user' 
    $pass = 'password'
    $folder = '\folder\folder1\'
    $target = "C:\temp\"

    #SET CREDENTIALS
    $credentials = new-object System.Net.NetworkCredential($user, $pass)

    function Get-FtpDir ($url,$credentials) {
        $request = [Net.WebRequest]::Create($url)
        $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
        if ($credentials) { $request.Credentials = $credentials }
        $response = $request.GetResponse()
        $reader = New-Object IO.StreamReader $response.GetResponseStream() 
        $reader.ReadToEnd()
        $reader.Close()
        $response.Close()
    }

    #SET FOLDER PATH
    $folderPath= $ftp + "/" + $folder + "/"

    $Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
    $files = ($Allfiles -split "`r`n")

    $files 

    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
    $counter = 0
    foreach ($file in ($files | where {$_ -like "*.*"})){
        $source=$folderPath + $file  
        $destination = $target + $file 
        $webclient.DownloadFile($source, $target+$file)

        #PRINT FILE NAME AND COUNTER
        $counter++
        $counter
        $source
    }
}

但是现在我试图将此Powershell脚本插入C#应用程序中。我走了这么远:

static void Main(string[] args)
        {                       
            string ftp = "ftp://my.server.ftp";
            string user = "ftp_user";
            string pass = "password";
            string folder = "\\folder\\folder1\\";
            string target = "C:\\temp\\";

            NetworkCredential credentials = new NetworkCredential(user, pass);

            string folderPath = ftp + "/" + folder + "/";

            var AllFiles = GetDir(folderPath, credentials);
            ??? $files = ($Allfiles -split "`r`n") ???
        }

        public static Object GetDir(string url, NetworkCredential credentials)
        {
            WebRequest request = WebRequest.Create(url);
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = credentials;
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            reader.ReadToEnd();
            reader.Close();
            response.Close();           
        } 

在powershell脚本中,“ Get-FtpDir”函数我不知道它返回什么对象,因此我不知道如何翻译这两个语句:

$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
$files = ($Allfiles -split "`r`n")

有人可以帮我吗?

提前感谢

c# powershell ftp
1个回答
1
投票

为什么在C#应用程序中使用Powershell脚本?

这里是到Microsoft How to: Download files with FTP的链接

这里是http://www.csidata.com/?page_id=2828中的另一个示例代码

using System.Net;
using System.IO;

String RemoteFtpPath = "ftp://ftp.csidata.com:21/Futures.20150305.gz";
String LocalDestinationPath = "Futures.20150305.gz";
String Username="yourusername";
String Password = "yourpassword";
Boolean UseBinary = true; // use true for .zip file or false for a text file
Boolean UsePassive = false;

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(RemoteFtpPath);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.KeepAlive = true;
request.UsePassive = UsePassive;
request.UseBinary = UseBinary;

request.Credentials = new NetworkCredential(Username, Password);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

using (FileStream writer = new FileStream(LocalDestinationPath, FileMode.Create))
{

    long length = response.ContentLength;
    int bufferSize = 2048;
    int readCount;
    byte[] buffer = new byte[2048];

    readCount = responseStream.Read(buffer, 0, bufferSize);
    while (readCount > 0)
    {
        writer.Write(buffer, 0, readCount);
        readCount = responseStream.Read(buffer, 0, bufferSize);
    }
}

reader.Close();
response.Close();
© www.soinside.com 2019 - 2024. All rights reserved.