Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello -

Here is what I have. A directory of files:

string[] filePaths = Directory.GetFiles(@"c:\files\", "*.*");


What I need to do is to use the File Signature to determine what the file type of each file is as I loop through, i.e. use the File Signature to determine if it's a .pdf (0xFFD8) or whatever.

Also I need to store the MD5 hash contents of each file as it is looping into a local variable.

I need to convert the file names stored in a string to actual files in order to glean this information.

How would I do this?

What I have tried:

not sure what to try - at a loss here
Posted
Updated 8-Apr-20 12:46pm

1 solution

First part is that you now have a list of file, which you seem to have

Part 2 is going to loop through those files (foreach), and within that loop...
2a: read each file into a Byte() array
2b: check the "magic number" from those file contents
2c: MD5 those same file contents

This question from a few years ago takes care of 2a/2b... And running an MD5 on that should not take too much more on your part
https://www.codeproject.com/Questions/1096579/Checking-file-signature-magic-number
 
Share this answer
 
Comments
Pita32 8-Apr-20 19:56pm    
Sorry MadMyche, could I trouble you to translate that into C#? I'm not yet too adept at translating VB.NET to C#.
MadMyche 8-Apr-20 21:47pm    
VB# is not that much different; and I would recommend that you use the MS Docs website to look up the methods.
Here is the Directory.GetFiles method:
https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netframework-4.8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900