FtpWebRequest.Method 屬性

定義

取得或設定要傳送至 FTP 伺服器的命令。

C#
public override string Method { get; set; }

屬性值

String 值,包含要傳送至伺服器的 FTP 命令。 預設值是 DownloadFile

例外狀況

由於已經在進行的要求,已為這個屬性指定新的值。

此方法無效。

-或-

不支援此方法。

-或-

指定多個方法。

範例

下列程式代碼範例會將這個屬性設定為 DeleteFile

C#
public static bool DeleteFileOnServer(Uri serverUri)
{
    // The serverUri parameter should use the ftp:// scheme.
    // It contains the name of the server file that is to be deleted.
    // Example: ftp://contoso.com/someFile.txt.
    //

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.DeleteFile;

    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
    Console.WriteLine("Delete status: {0}",response.StatusDescription);
    response.Close();
    return true;
}

備註

屬性 Method 會決定要傳送至伺服器的命令。 您可以使用 類別的WebRequestMethods.Ftp公用欄位成員中定義的字串來設定 Method 。 請注意,類別中 WebRequestMethods.Ftp 定義的字串是唯一支援的屬性選項 Method 。 將 Method 屬性設定為任何其他值將會導致 ArgumentException 例外狀況。

將 設定 MethodUploadFile時,您必須先這麼做, GetRequestStream 才能呼叫 方法。 當您嘗試取得要求數據流時,無法以正確的順序呼叫這些成員會造成 ProtocolViolationException 例外狀況。

FtpWebRequest 物件提供的認證必須具有執行指定方法的許可權。 如果沒有,FTP 命令會失敗。

若要判斷命令的成功或失敗,請檢查 StatusCodeStatusDescription 屬性。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱