Torna al Thread
void Main()
{
Console.WriteLine("lettura ftp");
var list = ListDirectory(new Uri("ftp://ftp.kernel.org/pub/linux/kernel/v3.x/"));
Console.WriteLine("\ntutti\n");
foreach (var element in list)
{
Console.WriteLine(element);
}
Console.WriteLine("\nsolo quelli da cancellare\n");
foreach (var element in list.Where (t => t.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase)))
{
Console.WriteLine(element);
DeleteFileOnServer(new Uri(string.Format("ftp://ftp.kernel.org/pub/linux/kernel/v3.x/{0}", element)));
}
}
// http://msdn.microsoft.com/en-us/library/ms229716(v=vs.110).aspx
public string[] ListDirectory(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 new string[0];
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.ListDirectory;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
var dirList = new List<string>();
string line;
while ((line = reader.ReadLine()) != null)
{
dirList.Add(line);
}
reader.Close();
response.Close();
return dirList.ToArray();
}
// http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest(v=vs.110).aspx
public 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;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
FtpWebResponse response = (FtpWebResponse) request.GetResponse();
Console.WriteLine("Delete status: {0}",response.StatusDescription);
response.Close();
return true;
}