compare a local file with another on ftp server c # [closed]

-1

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;
        }
    }
}
    
asked by LopezAi 03.11.2017 в 11:28
source

1 answer

1

Buenas LopezAi,

You have a failure in the following line, it may be because of this that the code does not work well:

if (FtpGetFileTimestamp(ftpWebRequest.ToString(), username, password) == FtpGetFileSize(sourceStream.ToString(), username, password))

You are comparing the value that returns FtpGetFileTimestamp with the value that returns FtpGetFileSize .

It should be like this:

if (FtpGetFileTimestamp(ftpWebRequest.ToString(), username, password) == FtpGetFileTimestamp(sourceStream.ToString(), username, password))

For another band, what you are looking for is to compare a local file with one on a server FTP , therefore, you can not use the FTP methods for the local file. You should use the library System.IO for the information of the local file, and it should look like this:

System.IO.FileInfo fi = new FileInfo(@"C:\Projects\BackUp_\Configurator.exe");
if(FtpGetFileSize(ftpWebRequest.ToString(), username, password) == fi.Lenght)
{
    if (FtpGetFileTimestamp(ftpWebRequest.ToString(), username, password) == fi.LastWriteTime)
    
answered by 03.11.2017 в 11:36