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

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 = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
   request->Method = WebRequestMethods::Ftp::DeleteFile;
   FtpWebResponse^ response = dynamic_cast<FtpWebResponse^>(request->GetResponse());
   Console::WriteLine( "Delete status: {0}", response->StatusDescription );
   response->Close();
   return true;
}
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 屬性。

適用於

另請參閱