Export (0) Print
Expand All

FtpWebRequest.Method Property

Gets or sets the command to send to the FTP server.

Namespace:  System.Net
Assembly:  System (in System.dll)

public override string Method { get; set; }

Property Value

Type: System.String
A String value that contains the FTP command to send to the server. The default value is DownloadFile.

ExceptionCondition
InvalidOperationException

A new value was specified for this property for a request that is already in progress.

ArgumentException

The method is invalid.

- or -

The method is not supported.

- or -

Multiple methods were specified.

The Method property determines which command is sent to the server. You set the Method by using the strings defined in the public field members of the WebRequestMethods.Ftp class. Note that the strings defined in the WebRequestMethods.Ftp class are the only supported options for the Method property. Setting the Method property to any other value will result in an ArgumentException exception.

When setting Method to UploadFile, you must do so before calling the GetRequestStream method. Failure to call these members in the correct order causes a ProtocolViolationException exception when you attempt to get the request stream.

The credentials supplied for the FtpWebRequest object must have permission to perform the specified method. If not, the FTP command fails.

To determine the success or failure of a command, check the StatusCode and StatusDescription properties.

The following code example sets this property to 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;
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft