0

So i have a method that searches for specific file types and moves them into a folder, i want it to exclude some files. Currently having it exclude by file name, my question is how do i exclude by md5 hash in case the excluded files are renamed.

List<string> DeskTopFiles
    = Directory.GetFiles(filepath, "*.exe*", SearchOption.AllDirectories)
               .ToList();

foreach (string file in DeskTopFiles)
{
    if (Path.GetFileName(file).ToLower() != "Whatever.exe")
        FileInfo mFile = new FileInfo(file);
        if (new FileInfo(d + "\\FileHolder\\" + mFile.Name).Exists == false)
            mFile.MoveTo(d + "\\FileHolder\\" + mFile.Name);
    }
}

So this is the part im trying to get it to just check md5

if (Path.GetFileName(file).ToLower() != "Whatever.exe")

Edit: guess i have to check the md5 of my exe, so how would i go about stopping it from moving itself if its run from the desktop. Currently have it by != name.exe but i want it by md5 hash

user2184248
  • 27
  • 1
  • 9
  • 7
    To check if a file matches a given MD5 hash, you need to calculate that file's MD5 hash. There's no other way, unless you have that precalculated somewhere. – Mat Dec 24 '13 at 18:33
  • How would you like to check md5 without calculating it first? – MarcinJuraszek Dec 24 '13 at 18:34
  • How do you want to check a file's MD5 hash without calculating? Unless you have said files "indexed". – Rodrigo Silva Dec 24 '13 at 18:34
  • k, guess your right i have to calc it first, basically i dont want my program to move itself will its being run from the desktop. – user2184248 Dec 24 '13 at 18:35
  • It won't move, since it's beind used, an exception will be raised. – Rodrigo Silva Dec 24 '13 at 18:36
  • Yeah, and the exe will just crash – user2184248 Dec 24 '13 at 18:42
  • If you're trying to avoid the slowdown from calculating signatures (MD5, crc, etc.) you could do a tiered approach where you first compare a small range of bytes directly (like the first 1024 of the file). Then if those bytes are identical, compute the full signature. – cod3monk3y Dec 24 '13 at 18:42

1 Answers1

0

Given the further explanation of the OP, the aim is to exlclude the file itself from being moved, and since this wouldn't happen because it's being used, it would throw an exception, so I'd take another approach, check the file MD5 hash only when it's being used, this would greatly reduce the number of files checked, thus improving performance. To check if the file is being used, use exceptions:

    protected virtual bool IsFileLocked(FileInfo file)
{
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (IOException)
    {
        //the file is unavailable because it is:
        //still being written to
        //or being processed by another thread
        //or does not exist (has already been processed)
        return true;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}

Is there a way to check if a file is in use?

Furthermore, in your loop if the above function returns True, check its MD5 against a previously defined one (C# MD5 hasher example)

Community
  • 1
  • 1
Rodrigo Silva
  • 432
  • 1
  • 6
  • 18
  • Thank you, this makes sense. Now would i have to rewrite the method, or can i just modify- if (Path.GetFileName(file).ToLower() != "Whatever.exe") – user2184248 Dec 24 '13 at 18:49
  • I'm not sure what you mean, which part are you refering to? Or do you mean the entire method? – Rodrigo Silva Dec 24 '13 at 18:51
  • the entire method for moving files, and excluding just the one. Or can i just modify my if statement to add in what you gave me – user2184248 Dec 24 '13 at 18:53
  • Yes, you can just modify it, though I'd rewrite it. Check each file for its availability (is it being used?) if not, proceed as usual, if indeed it is being used check said file MD5 hash against your current file hash. Yet, since it's being used you are NOT able to move it (but you can copy it), so checking the MD5 hash would only make sense if you want to confirm that it is indeed the file/s you're looking for. – Rodrigo Silva Dec 24 '13 at 18:58
  • Just need to check the md5 of my .exe , the rest dont matter – user2184248 Dec 24 '13 at 19:01
  • In your loop, check if the file is being used, if so, check the name (if currentFileBeingProcessed == "yourfile.exe"), do nothing. But as I said before, this is useless, just check if the file is being used, because if it is it would NOT be moved anyways. Conclusion: don't move TRY to move files that are being used. – Rodrigo Silva Dec 24 '13 at 19:05