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

适用于

另请参阅