-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
64 lines (54 loc) · 2.79 KB
/
Copy pathProgram.cs
File metadata and controls
64 lines (54 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.IO;
using JNysteen.FileTypeIdentifier.MagicNumbers;
namespace JNysteen.FileTypeIdentifier.ConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
/***
* This simple console application contains an example of how the file type identifier is used.
* A file magic number mapping is instantiated and configured to contain the magic numbers for a PDF file.
* A file type identifier is instantiated with this mapping, enabling it to recognize PDF files.
*
* The input loop asks the user for a file path and, using the file type identifier, tells the user
* whether the provided file was a PDF or not.
*/
// Instantiate a mapping of the file magic numbers that should be recognized
var fileMagicNumberMapping = new MagicNumberMapping();
// Add the file magic numbers for the PDF file type
fileMagicNumberMapping.AddMagicNumbers(DocumentMagicNumbers.PDFMagicNumbers, DocumentMagicNumbers.PDF);
// If more types of files should be recognized, they can be added to the file magic number mapping here
// Instantiate a new file type identifier with the configure file magic number mapping
var fileTypeIdentifier = new FileTypeIdentifier(fileMagicNumberMapping);
while (true)
{
Console.WriteLine("Input the path to a file that should be identified: ");
var inputFilePath = Console.ReadLine();
if (string.IsNullOrWhiteSpace(inputFilePath))
{
Console.WriteLine("No input was provided...");
continue;
}
if (!File.Exists(inputFilePath))
{
Console.WriteLine($"File at path '{inputFilePath}' does not exist!");
continue;
}
string identifiedFileType = null;
// Open a read Stream for the provided file
using (var fileStream = File.OpenRead(inputFilePath))
{
identifiedFileType = fileTypeIdentifier.GetFileType(fileStream);
}
// Now, if the provided file is a PDF, it will be recognized by the file type identifier!
if (identifiedFileType != null)
Console.WriteLine($"File type recognized! The type of this file is '{identifiedFileType}'");
// If the file is not a PDF, the type is unknown to the identifier
else
Console.WriteLine("The file type identifier was not configured to recognize this type of file!");
}
}
}
}