I will save name and path of files in a database and I want to check
(before insert a new file) if a given file is in the database. I think
I could use hash codes. The class FileInfo has GetHashCode() method. Is
this method good enough for my task? Are there any other ways of doing
this task?
Thanks.
Actually I think that using the hashcode you are adding an extra step.
You have your filename, you can easily search on your database for that
string, if you get the hash then you also has to get the hash from the
database, so you have several extra steps. You must have the hash key on the
database as well in order to do it in the correct way, if this is the case
numbers are much fasters than strings to search so it will be the correct
scenario.
Note: There is a slight chance that two files have the same hash code... is
not guaranteed to be unique.
hope this helps
Salva
No - GetHashCode has nothing to do with the contents of the file, it's
for the purposes of putting objects into hashtables.
Have a look at the MD5 class, and in particular its ComputeHash method.
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
I did exactly this kind of thing in a project I wrote in Delphi.
Unfortunately, the fully qualified file name isn't enough to ensure that the
file hasn't been uploaded to the database.
The reason is that the user can use Windows explorer to File -> Copy ->
Paste and you have a brand new file name with the exact same contents.
Instead of using the fully qualified file name to identify whether the file
had been uploaded, I used an MD5 hash code.
The Delphi implementation can be downloaded here:
http://www.fichtner.net/delphi/md5.delphi.phtml
In my Delphi code, it was as simple as making a call like this:
MD5Value := MD5Print(MD5File(Filename)) ;
That gets the Message Digest hash code, and converts it to a string
representation, which you can then store as char/varchar in your database.
I have no ideas whether there is a C#/.net implementation, you might take a
look on sourceforge or open source related web sites.
Good luck,
Richard
--
Thanks
Wayne Sepega
Jacksonville, Fl
Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg
"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
"Richard Rogers" <rich...@NOSPAMrogers.com> wrote in message
news:o4SdnbQrZoS...@rogers.com...
public static string ComputeMD5Hash(string fileName)
{
return ComputeHash(fileName, new MD5CryptoServiceProvider());
}
public static string ComputeSHA1Hash(string fileName)
{
return ComputeHash(fileName, new SHA1CryptoServiceProvider());
}
public static string ComputeHash(string fileName, HashAlgorithm
hashAlgorithm)
{
FileStream stmcheck = File.OpenRead(fileName);
try
{
byte[] hash = hashAlgorithm.ComputeHash(stmcheck);
string computed = BitConverter.ToString(hash).Replace("-", "");
return computed;
}
finally
{
stmcheck.Close();
}
}
--
Thanks
Wayne Sepega
Jacksonville, Fl
Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg
"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
"Wayne" <MeNo...@community.nospam> wrote in message
news:e9hThOQR...@tk2msftngp13.phx.gbl...