共用方式為


FtpWebRequest.Method 屬性

定義

接收或設定指令傳送到 FTP 伺服器。

public:
 virtual property System::String ^ Method { System::String ^ get(); void set(System::String ^ value); };
public override string Method { get; set; }
member this.Method : string with get, set
Public Overrides Property Method As String

屬性值

一個 String 包含 FTP 指令的值,用來傳送給伺服器。 預設值為 DownloadFile

例外狀況

為已進行中的請求指定了該屬性的新值。

這個方法是無效的。

-或-

此方法不被支援。

-或-

具體規定了多種方法。

範例

以下程式碼範例將此屬性設定為 DeleteFile

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 屬性。

適用於

另請參閱