2010-11-14 7 views
11

Voglio ottenere la dimensione di un file su un FTP.Ottieni dimensioni file su un FTP in C#

 //Get File Size 
     reqSize = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpPath + filePath)); 
     reqSize.Credentials = new NetworkCredential(Username, Password); 
     reqSize.Method = WebRequestMethods.Ftp.GetFileSize; 
     reqSize.UseBinary = true; 
     FtpWebResponse respSize = (FtpWebResponse)reqSize.GetResponse(); 
     long size = respSize.ContentLength; 
     respSize.Close(); 

Ho provato quanto segue ma ottengo un errore 550. File non trovato/nessun accesso. Tuttavia, il seguente codice funziona ...

   reqTime = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpPath + filePath)); 
       reqTime.Credentials = new NetworkCredential(Username, Password); 
       reqTime.Method = WebRequestMethods.Ftp.GetDateTimestamp; 
       reqTime.UseBinary = true; 
       FtpWebResponse respTime = (FtpWebResponse)reqTime.GetResponse(); 
       DateTime LastModified = respTime.LastModified; 
       respTime.Close(); 

EDIT: La ragione per cui questo non sta funzionando per me è che il mio server FTP non supporta il metodo size.

risposta

22

Prova reqSize.Method = WebRequestMethods.Ftp.GetFileSize; invece di GetDateTimestamp

Questo ha funzionato per me:

+0

Commento migliore del precedente: Ha bisogno di leggere i dati dalla risposta, non solo di ottenere "ContentLength", credo. Ad ogni modo, sembra strano che il contentlength sia 0. –

+0

Quello era un errore di copia incolla - Ho aggiornato la mia domanda con molti più dettagli. – Jason

+0

Posso ottenere la dimensione del file senza scaricare il file giusto? Non voglio scaricare questo file, dal momento che è enorme, se ha le stesse dimensioni localmente. – Jason

0

// più semplice e modo efficiente per ottenere Dimensioni del file FTP.

var size = GetFtpFileSize (nuovo Uri ("ftpURL"), nuovo NetworkCredential ("userName", "password"));

public static long GetFtpFileSize(Uri requestUri, NetworkCredential networkCredential) 
{ 
    //Create ftpWebRequest object with given options to get the File Size. 
    var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.GetFileSize); 

    try { return ((FtpWebResponse)ftpWebRequest.GetResponse()).ContentLength; } //Incase of success it'll return the File Size. 
    catch (Exception) { return default(long); } //Incase of fail it'll return default value to check it later. 
} 
public static FtpWebRequest GetFtpWebRequest(Uri requestUri, NetworkCredential networkCredential, string method = null) 
{ 
    var ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri); //Create FtpWebRequest with given Request Uri. 
    ftpWebRequest.Credentials = networkCredential; //Set the Credentials of current FtpWebRequest. 

    if (!string.IsNullOrEmpty(method)) 
     ftpWebRequest.Method = method; //Set the Method of FtpWebRequest incase it has a value. 
    return ftpWebRequest; //Return the configured FtpWebRequest. 
}