7

.exeファイルをアップロード/ダウンロードするプログラムを作成しようとしていますFTP

を使用してみFtpWebRequestましたが、.txtファイルのアップロードとダウンロードにしか成功しません。

それから私はここで:を使用してダウンロードするための解決策を見つけましたWebClient

WebClient request = new WebClient();
request.Credentials = new NetworkCredential("username", "password");
byte[] fileData =  request.DownloadData("ftp://myFTP.net/");

FileStream file = File.Create(destinatie);
file.Write(fileData, 0, fileData.Length);

file.Close();

このソリューションは機能します。しかし、私はそれがうまくいかなかっWebClientた方法を持っているのを見ました。だけでDownloadFileは動作しないからだと思います。私の仮定は本当ですか?そうでない場合、どうすればそれを機能させることができますか?FTPHTTP

.exeそして、ftpにファイルをアップロード/ダウンロードするための他の解決策はありますFtpWebRequestか?

4

3 に答える 3

15

と の両方が、FTPWebClient.UploadFileおよびWebClient.DownloadFileバイナリ ファイルに対して正しく機能します。


アップロード

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile(
    "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

TLS/SSL 暗号化、ASCII/テキスト転送モード、転送再開など、WebClient提供されていないより詳細な制御が必要な場合は、 を使用します。簡単な方法は、次を使用して FTP ストリームにコピーすることです。FtpWebRequestFileStreamStream.CopyTo

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;  

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    fileStream.CopyTo(ftpStream);
}

アップロードの進行状況を監視する必要がある場合は、コンテンツを自分でチャンクごとにコピーする必要があります。

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;  

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        ftpStream.Write(buffer, 0, read);
        Console.WriteLine("Uploaded {0} bytes", fileStream.Position);
    } 
}

GUI の進行状況 (WinForms ProgressBar) については、次を参照してください:
FtpWebRequest でアップロードの進行状況バーを表示するにはどうすればよいですか?

フォルダーからすべてのファイルをアップロードする場合は
、C# での FTP サーバーへの再帰的アップロード を参照してください。


ダウンロード

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
    "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

TLS/SSL 暗号化、ASCII/テキスト転送モード、転送の再開WebClientなど、提供されていないより詳細な制御が必要な場合は、 を使用します。簡単な方法は、FTP 応答ストリームを次のようにコピーすることです。FtpWebRequestFileStreamStream.CopyTo

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    ftpStream.CopyTo(fileStream);
}

ダウンロードの進行状況を監視する必要がある場合は、コンテンツを自分でチャンクごとにコピーする必要があります。

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fileStream.Write(buffer, 0, read);
        Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
    }
}

GUI の進行状況 (WinForms ProgressBar) については、以下を参照してください:
FtpWebRequest ProgressBar を使用した FTP ダウンロード

リモート フォルダーからすべてのファイルをダウンロードする場合は、
C# Download all files and subdirectories through FTP を参照してください。

于 2017-08-02T05:38:07.347 に答える
3

アップロードは簡単...

void Upload(){
    Web client = new WebClient();
    client.Credentials = new NetworkCredential(username, password);
    client.BaseAddress = "ftp://ftpsample.net/";

    client.UploadFile("sample.txt", "sample.txt"); //since the baseaddress
}


ダウンロードも簡単です... 以下で作成したコードはBackgroundWorkerを使用しています (したがって、ダウンロードの開始時にインターフェイス/アプリケーションがフリーズしません)。さらに、lambda (=>)を使用してコードを短縮しました。

void Download(){
    Web client = new WebClient();
    client.Credentials = new NetworkCredential(username, password);

    BackgroundWorker bg = new BackgroundWorker();
    bg.DoWork += (s, e) => { 
        client.DownloadFile("ftp://ftpsample.net/sample.zip", "sample.zip"); 
    };
    bg.RunWorkerCompleted += (s, e) => { //when download is completed
        MessageBox.Show("done downloading");
        download1.Enabled = true; //enable download button
        progressBar.Value = 0; // reset progressBar
    };
    bg.RunWorkerAsync();
    download1.Enabled = false; //disable download button 
    while (bg.IsBusy)
    {
        progressBar.Increment(1); //well just for extra/loading
        Application.DoEvents(); //processes all windows messages currently in the message queue
    }
}

DownloadFileAsyncメソッドを使用して、ファイルのダウンロードの実際の進行状況を表示することもできます。

client.DownloadFileAsync(new Uri("ftp://ftpsample.net/sample.zip"), "sample.zip");
client.DownloadProgressChanged += (s, e) =>
{
    progressBar.Value = e.ProgressPercentage; //show real download progress
};
client.DownloadFileCompleted += (s, e) =>
{
    progressBar.Visible = false;
    // other codes after the download
};
//You can remove the 'while' statement and use this ^
于 2016-03-12T04:12:08.993 に答える
3

アップロードするファイルがテキスト ファイルかバイナリ ファイルかを指定する必要があります。リクエストが宣言されて初期化された後に、次の行を追加します。

request.UseBinary = true;

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx

于 2012-10-28T14:50:54.633 に答える