I have to compare two files, one in local and one in ftp server. The first thing is to compare them by name, if the names do not match I have to upload the local file to the server. If the names match I have to compare them by date of last modification and by size, in case the two comparisons are the same I do not have to do anything, and if the comparisons do not match I have to upload the file to the ftp server.
I leave the code I have so far:
public static void FtpUpdateOrNot(string direccionIP, string username, string password)
{
// Get the object used to communicate with the server
Uri uri = new Uri(string.Concat("ftp://", direccionIP, "/Configurator.exe"));
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uri);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon
request.Credentials = new NetworkCredential(username, password);
StreamReader sourceStream = new StreamReader(@"C:\Projects\BackUp_\Configurator.exe");
Console.WriteLine("The name of the files are the same.");
if(FtpGetFileSize(ftpWebRequest.ToString(),username,password)==FtpGetFileSize(sourceStream.ToString(),username,password))
{
if (FtpGetFileTimestamp(ftpWebRequest.ToString(), username, password) == FtpGetFileTimestamp(sourceStream.ToString(), username, password))
{
Console.WriteLine("We don´t need to update");
}
}
else
{
// Copy the contents of the file to the request stream
byte[] fileContents = File.ReadAllBytes(@"C:\Projects\BackUp_\Configurator.exe");
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
//if the name are not the same
else
{
// Copy the contents of the file to the request stream
byte[] fileContents = File.ReadAllBytes(@"C:\Projects\BackUp_\Configurator.exe");
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
// Use FTP to get a remote file's size
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.
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;
}
}
// Use FTP to get a remote file's timestamp
static DateTime FtpGetFileTimestamp(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.GetDateTimestamp;
// Get network credentials
request.Credentials =
new NetworkCredential(username, password);
try
{
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
// Return the size.
return response.LastModified;
}
}
catch (Exception ex)
{
// If the file doesn't exist, return Jan 1, 3000
// Otherwise rethrow the error
if (ex.Message.Contains("File unavailable"))
return new DateTime(3000, 1, 1);
throw;
}
}
}