Read a folder file and determine if it is the same as the website

Asked

Viewed 62 times

0

I’m doing an Auncher autoupdate on c# . net and I need my program to read a database file that’s in the same folder as it is and see if it’s the same as my site, and if it’s different, it download.

Does anyone know if this is possible?

  • Yes, one of the outputs is to use FTP connection

  • What is db? you want to compare the structure or content as well?

  • Your question leaves a question, what exactly is this "database file"? It’s a database file, a Access for example?

  • 1

    It’s a database of a game I’m making. But I’ve already managed with Rovann Linhalis’s md5 method, thank you very much to everyone who tried to help!! I’m very grateful indeed ^^

1 answer

1

In the local application you get the md5 of 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 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']);
?>

Utilizing:

 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 one file. If there are several files, the logic is the same, but the md5.php It 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 quantities followed by requests.

  • I got it with your method, it worked exactly as I wanted. You pretty much did all the work by yourself, sorry kkkk Thank you very much!! I can’t thank you enough ^^

  • @Murdock rsrs, mark as answer =] for nothing!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.