sintassi:
WebClient oggetto = new WebClient(); crea oggetto connessione
oggetto.Credentials = new NetworkCredential("user", "password"); setta user e password
oggetto.BaseAddress = "ftp://ftp.xxxx.xx/"; inserisce FTP
oggetto.UploadFile("path remoto\nome file destinazione remoto", "path\file origine locale"); upload di un file
oggetto.DownloadFile("path remoto\nome file origine remoto", "path\file destinazione locale"); download di un file
sintassi:
var richiesta = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxx.xx/directory/file"); creo oggetto del file remoto ftp
richiesta.Credentials = new NetworkCredential("user", "password"); setta user e password
richiesta.Method = WebRequestMethods.Ftp.metodoFTP; richiesta FTP del metodo sul file
FtpWebResponse risposta = (FtpWebResponse)richiesta.GetResponse(); analizza ed esegue la richiesta
risposta.Close(); chiude la richiesta
metodi FTP: (sito Microsoft)
esempio: verifico se esiste il file ed in caso restituisco true
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxx.xx/directory/file.txt");
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();}
response.Close();
return true;
return false;}
esempio: dopo la verifica cancello il file
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxx.xx/directory/file.txt");
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
esempio: creo la direcotry "directory"
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxx.xx/" + "directory");
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
funzione che verifica se la path directory passata esiste
public bool VerificaFTPDirectory(string dirPath)
{
try}
{FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);}
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.ListDirectory;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{return true;}
catch (WebException ex)
{if (ex.Response != null)}
{FtpWebResponse response = (FtpWebResponse)ex.Response;}
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{return false;}
funzione che restituisce una lista con files e direcory presenti nella path passata
List<string> lista = GetAllFtpFiles("ftp://ftp.xxxxx.xx/subdir/");
private static List<string> GetAllFtpFiles(string ParentFolderpath)
{
try}
{FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ParentFolderpath);}
ftpRequest.Credentials = new NetworkCredential("user", "password");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{var lineArr = line.Split('/');}
line = lineArr[lineArr.Count() - 1];
directories.Add(line);
line = streamReader.ReadLine();
streamReader.Close();
return directories;
catch (Exception ex)
{throw ex;}
esempio: cancello la directory "directory"
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.xxx.xx/" + "directory");
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.RemoveDirectory;
request.GetResponse().Close();