-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHasher.xaml.cs
More file actions
138 lines (115 loc) · 6.79 KB
/
FileHasher.xaml.cs
File metadata and controls
138 lines (115 loc) · 6.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using Microsoft.WindowsAPICodePack.Dialogs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
namespace FileHasher
{
public partial class FileHasher : Window
{
public static configs Config { get; set; } = new();
public FileHasher()
{
InitializeComponent();
DataContext = Config;
}
public void GuiUpdater()
{
TextBoxResult.Text = Config.StringResult;
TextStartPath.Text = Config.StartPath;
TextStartDlLink.Text = Config.StartDlLink;
}
private CommonOpenFileDialog openFileDialog = new CommonOpenFileDialog() { Multiselect = true, AllowNonFileSystemItems = true, EnsureFileExists = true, EnsurePathExists = true, EnsureReadOnly = true, EnsureValidNames = true, IsFolderPicker = true, NavigateToShortcut = true, ShowHiddenItems = true, ShowPlacesList = true, Title = "Select what you want to checksum", InitialDirectory = Config.StartPathCkecked && Directory.Exists(Config.StartPath) ? Config.StartPath : null };
private void BtnGetDirectory_Click(object sender, RoutedEventArgs e)
{
if (openFileDialog.ShowDialog() != CommonFileDialogResult.Ok)
{
MessageBox.Show("Nothing selected");
return;
}
ListOfFiles.Clear();
Config.StringResult = "Hashing all files. The process may take some time and the screen may freeze in the meantime. hold...";
GuiUpdater();
foreach (string filePath in openFileDialog.FileNames)
{
if (File.Exists(filePath))
{
ListOfFiles.Add(new FileParameters() { DlLink = Config.StartDlLinkCkecked && Uri.IsWellFormedUriString(Config.StartDlLink, UriKind.Absolute) && File.Exists(filePath) && Config.StartPathCkecked && Directory.Exists(Config.StartPath) ? new Uri(new Uri(Config.StartDlLink, UriKind.Absolute), Path.GetRelativePath(Config.StartPath + '/', filePath), false).AbsoluteUri : null, FileName = Path.GetFileName(filePath), Directory = Config.StartPathCkecked ? Directory.Exists(Config.StartPath) ? Path.GetDirectoryName(Path.GetRelativePath(Config.StartPath, filePath)).Replace(@"//", @"/").Replace(@"\\", @"\").Replace(@"\", @"/").TrimStringAtEnd(Path.GetFileName(filePath)) : null : Directory.Exists(Config.StartPath) ? Path.GetDirectoryName(Path.GetFullPath(filePath)).Replace(@"//", @"/").Replace(@"\\", @"\").Replace(@"\", @"/").TrimStringAtEnd(Path.GetFileName(filePath)) : null, Sha256 = File.Exists(filePath) ? FileSHA256(filePath) : null }); ;
}
else if (Directory.Exists(filePath))
{
foreach (string filePathInSubdir in Directory.GetFiles(filePath, "*", SearchOption.AllDirectories))
{
ListOfFiles.Add(new FileParameters() { DlLink = Config.StartDlLinkCkecked && Uri.IsWellFormedUriString(Config.StartDlLink, UriKind.Absolute) && File.Exists(filePathInSubdir) && Config.StartPathCkecked && Directory.Exists(Config.StartPath) ? new Uri(new Uri(Config.StartDlLink, UriKind.Absolute), Path.GetRelativePath(Config.StartPath + '/', filePathInSubdir), false).AbsoluteUri : null, FileName = Path.GetFileName(filePathInSubdir), Directory = Config.StartPathCkecked ? Directory.Exists(Config.StartPath) ? Path.GetDirectoryName(Path.GetRelativePath(Config.StartPath, filePathInSubdir)).Replace(@"//", @"/").Replace(@"\\", @"\").Replace(@"\", @"/").TrimStringAtEnd(Path.GetFileName(filePath)) : null : Directory.Exists(Config.StartPath) ? Path.GetDirectoryName(Path.GetFullPath(filePathInSubdir)).Replace(@"//", @"/").Replace(@"\\", @"\").Replace(@"\", @"/").TrimStringAtEnd(Path.GetFileName(filePath)) : null, Sha256 = File.Exists(filePathInSubdir) ? FileSHA256(filePathInSubdir) : null }); ;
}
}
}
ListOfFiles = ListOfFiles.OrderBy(x => x.FileName).ToList();
ListOfFiles = ListOfFiles.OrderBy(x => x.Directory).ToList();
if (ListOfFiles.Count == 0) { Config.StringResult = "No files in selected directories"; }
else
{
Config.StringResult = JsonConvert.SerializeObject(ListOfFiles, Formatting.Indented);
}
GuiUpdater();
}
public static string FileSHA256(string filePath)
{
using System.Security.Cryptography.SHA256 hashAlgorithm = System.Security.Cryptography.SHA256.Create();
using (FileStream stream = System.IO.File.OpenRead(filePath))
{
return BitConverter.ToString(hashAlgorithm.ComputeHash(stream)).Replace("-", "").ToUpper();
}
}
public static List<FileParameters> ListOfFiles { get; set; } = new();
public partial class FileParameters
{
[JsonProperty("dlLink", NullValueHandling = NullValueHandling.Ignore)]
public string DlLink { get; set; }
[JsonProperty("directory", NullValueHandling = NullValueHandling.Ignore)]
public string Directory { get; set; }
[JsonProperty("fileName", NullValueHandling = NullValueHandling.Ignore)]
public string FileName { get; set; }
[JsonProperty("sha256", NullValueHandling = NullValueHandling.Ignore)]
public string Sha256 { get; set; }
}
public class configs
{
public bool StartPathCkecked { get; set; } = false;
public bool StartDlLinkCkecked { get; set; } = false;
public string StartPath { get; set; } = @"C:\";
public string StartDlLink { get; set; } = @"https://";
public string StringResult { get; set; } = "Select files to show result";
}
private void BtnGetStartDirectory_Click(object sender, RoutedEventArgs e)
{
CommonOpenFileDialog cfd = new CommonOpenFileDialog
{
EnsurePathExists = true,
EnsureFileExists = false,
AllowNonFileSystemItems = true,
Multiselect = false,
IsFolderPicker = true,
Title = "Select a Directory"
};
if (cfd.ShowDialog() != CommonFileDialogResult.Ok)
{
MessageBox.Show("No directory selected");
return;
}
Config.StartPath = Directory.Exists(cfd.FileName) ? cfd.FileName : Path.GetDirectoryName(cfd.FileName);
GuiUpdater();
}
}
public static class Tools
{
public static string TrimStringAtEnd(this string source, string value)
{
if (!source.EndsWith(value))
return source;
return source.Remove(source.LastIndexOf(value));
}
}
}