Read file from a folder and determine if it is the same as the site [closed]

0

I'm doing an autoupdate launcher in C # .net and I need my program to read a database file that is in the same folder as it and see if it is the same as my site, and if it is different, p>

Does anyone know if this is possible?

    
asked by anonymous 20.02.2018 / 10:37

1 answer

1

In the local application you get md5 from the file you need:

public static string Md5FromFile(string input)
{
    string saida = null;
    using (System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create())
    {
        Byte[] bytes = System.IO.File.ReadAllBytes(input);
        Byte[] data = md5Hasher.ComputeHash(bytes);
        StringBuilder sBuilder = new StringBuilder();
        foreach (var valorByte in data)
            sBuilder.Append(valorByte.ToString("x2"));

        saida = sBuilder.ToString();
    }
    return saida.ToString();
}

and the Md5 of the file that is on the site:

public string GetMd5FromSite(string arquivo)
{
    NameValueCollection nvc;
    nvc = new NameValueCollection();
    nvc.Add("arquivo", arquivo);
    System.Net.WebClient client = new System.Net.WebClient();
    string URL = "http://www.seudominio.com.br/pasta/md5.php";
    WebProxy myProxy = new WebProxy();
    myProxy.BypassProxyOnLocal = true;
    client.Proxy = myProxy;
    byte[] responseArray = client.UploadValues(URL, nvc);
    return new System.Text.ASCIIEncoding().GetString(responseArray);
}

md5.php

<?php
function fmd5($p_arquivo)
{
    return md5_file($p_arquivo);
}

echo fmd5($_POST['arquivo']);
?>

Usage:

 string md5Local = Md5FromFile(Application.StartupPath+"\arquivo.txt");
 string md5Site = GetMd5FromSite("arquivo.txt"); //considerando que o arquivo está no mesmo diretório do md5.php

 if (md5Local != md5Site)
 {
     //faz o download
 }
  

As my server works with php, I used this feature as an example. Following the same logic you also use another language.

  

This example works for a file. If they are multiple files, the logic is the same, however the md5.php is different so that in only one request all md5 of an informed folder are returned. Otherwise the server may block your connection for the required number of requests.

    
20.02.2018 / 12:43