8

FtpWebRequestC#のクラスを使用して、アップロード、ダウンロード、削除などの機能を実装しました。それはかなり簡単です。

私が今する必要があるのは、次のような任意のFTPコマンドの送信をサポートすることです。

quote SITE LRECL=132 RECFM=FB
or 
quote SYST

これが私たちの構成例app.configです:

<!-- The following commands will be executed before any uploads occur -->
<extraCommands>
     <command>quote SITE LRECL=132 RECFM=FB</command>
</extraCommands>

私はまだこれを使用してこれを行う方法を研究していますFtpWebRequestWebClient次は授業をやってみます。誰でも私を正しい方向に早く向けることができますか?ありがとう!

FtpWebRequest更新:.NET Framework 3.5の時点では、にあるもの以外は何もサポートしていないので、同じ結論に達しましたWebRequestMethods.Ftp.*。他の投稿で推奨されているサードパーティのアプリを試してみます。助けてくれてありがとう!

4

4 に答える 4

9

私はそれがでできるとは思わないFtpWebRequest...FTPコマンドを指定する唯一の方法はMethodプロパティを介することであり、ドキュメントには次のように記載されています。

WebRequestMethods.Ftpクラスで定義された文字列は、Methodプロパティでサポートされている唯一のオプションであることに注意してください。Methodプロパティを他の値に設定すると、ArgumentException例外が発生します。

SITEとSYSTは事前定義されたオプションに含まれていないので、行き詰まっていると思います...

クラスを試すために時間を無駄にしないでくださいWebClient。それはあなたにさらに少ない柔軟性を与えますFtpWebRequest

ただし、オープンソースまたは商用のサードパーティ製FTP実装はたくさんあり、そのうちのいくつかはカスタムコマンドを処理できると確信しています...

于 2010-02-23T21:17:43.477 に答える
7

トーマス・レベスクが彼の答えで言ったように、はあなたFtpWebRequest助けません。いくつかのサードパーティソリューション、またはVisualBasicで記述された回答からリファクタリングした次の簡略化されたベースのコードを使用できます。TcpClient

public static void SendFtpCommand()
{
    var serverName = "[FTP_SERVER_NAME]";
    var port = 21;
    var userName = "[FTP_USER_NAME]";
    var password = "[FTP_PASSWORD]"
    var command = "SITE CHMOD 755 [FTP_FILE_PATH]";

    var tcpClient = new TcpClient();
    try
    {
        tcpClient.Connect(serverName, port);
        Flush(tcpClient);

        var response = TransmitCommand(tcpClient, "user " + userName);
        if (response.IndexOf("331", StringComparison.OrdinalIgnoreCase) < 0)
            throw new Exception(string.Format("Error \"{0}\" while sending user name \"{1}\".", response, userName));

        response = TransmitCommand(tcpClient, "pass " + password);
        if (response.IndexOf("230", StringComparison.OrdinalIgnoreCase) < 0)
            throw new Exception(string.Format("Error \"{0}\" while sending password.", response));

        response = TransmitCommand(tcpClient, command);
        if (response.IndexOf("200", StringComparison.OrdinalIgnoreCase) < 0)
            throw new Exception(string.Format("Error \"{0}\" while sending command \"{1}\".", response, command));
    }
    finally
    {
        if (tcpClient.Connected)
            tcpClient.Close();
    }
}

private static string TransmitCommand(TcpClient tcpClient, string cmd)
{
    var networkStream = tcpClient.GetStream();
    if (!networkStream.CanWrite || !networkStream.CanRead)
        return string.Empty;

    var sendBytes = Encoding.ASCII.GetBytes(cmd + "\r\n");
    networkStream.Write(sendBytes, 0, sendBytes.Length);

    var streamReader = new StreamReader(networkStream);
    return streamReader.ReadLine();
}

private static string Flush(TcpClient tcpClient)
{
    try
    {
        var networkStream = tcpClient.GetStream();
        if (!networkStream.CanWrite || !networkStream.CanRead)
            return string.Empty;

        var receiveBytes = new byte[tcpClient.ReceiveBufferSize];
        networkStream.ReadTimeout = 10000;
        networkStream.Read(receiveBytes, 0, tcpClient.ReceiveBufferSize);

        return Encoding.ASCII.GetString(receiveBytes);
    }
    catch
    {
        // Ignore all irrelevant exceptions
    }

    return string.Empty;
}

FTPを通過する間、次のフローが期待できます。

220 (vsFTPd 2.2.2)
user [FTP_USER_NAME]
331 Please specify the password.
pass [FTP_PASSWORD]
230 Login successful.
SITE CHMOD 755 [FTP_FILE_PATH]
200 SITE CHMOD command ok.
于 2015-09-29T15:19:11.040 に答える
3

RebexFTPコンポーネントを試すことができます:

// create client and connect 
Ftp client = new Ftp();
client.Connect("ftp.example.org");
client.Login("username", "password");

// send SITE command
// note that QUOTE and SITE are ommited. QUOTE is command line ftp syntax only.
client.Site("LRECL=132 RECFM=FB");

// send SYST command
client.SendCommand("SYST");
FtpResponse response = client.ReadResponse();
if (response.Group != 2) 
  ; // handle error 

// disconnect 
client.Disconnect();
于 2010-02-24T17:05:44.413 に答える
-5

使用するsendCommand("SITE LRECL=242 BLKSIZE=0 RECFM=FB");

于 2010-06-10T10:29:46.173 に答える