Tengo que obtener el tamaño de un archivo de ftp. Me aparece este error
Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.FtpWebRequest'
en la línea que creo el objeto FtpWebRequest. Os dejo el código que tengo de momento:
public static long FtpGetFileSize(string uri, string username, string password)
{
// Get the object used to communicate with the server
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.GetFileSize;
// Get network credentials.
request.Credentials = new NetworkCredential(username, password);
try
{
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
// Return the size.
Console.WriteLine("El tamaño del archivo es {0}",response.ContentLength);
return response.ContentLength;
}
}
catch (Exception ex)
{
// If the file doesn't exist, return -1
// Otherwise rethrow the error
if (ex.Message.Contains("File unavailable")) return -1;
throw;
}
}