|
| 1 | +using Microsoft.Win32; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows; |
| 11 | +using System.Windows.Controls; |
| 12 | +using System.Windows.Data; |
| 13 | +using System.Windows.Documents; |
| 14 | +using System.Windows.Input; |
| 15 | +using System.Windows.Media; |
| 16 | +using System.Windows.Media.Imaging; |
| 17 | +using System.Windows.Navigation; |
| 18 | + |
| 19 | + |
| 20 | +namespace get_files_hash_checksum |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Interaction logic for MainWindow.xaml |
| 24 | + /// </summary> |
| 25 | + public partial class MainWindow : Window |
| 26 | + { |
| 27 | + public MainWindow() |
| 28 | + { |
| 29 | + InitializeComponent(); |
| 30 | + DataContext = this; |
| 31 | + } |
| 32 | + |
| 33 | + public bool startPathCkecked { get; set; } = false; |
| 34 | + public bool startDlLinkCkecked { get; set; } = false; |
| 35 | + public string startPath { get; set; } = @"C:\"; |
| 36 | + public string startDlLink { get; set; } = @"https://"; |
| 37 | + public OpenFileDialog selectedFiles { get; set; } = null; |
| 38 | + public string stringResult { get; set; } = "Select files to show result"; |
| 39 | + |
| 40 | + |
| 41 | + private void btnGetDirectory_Click(object sender, RoutedEventArgs e) |
| 42 | + { |
| 43 | + ListOfFiles.Clear(); |
| 44 | + |
| 45 | + OpenFileDialog openFileDialog = new OpenFileDialog() { Multiselect = true, CheckFileExists = true, CheckPathExists = true, Title = "Select what you want to checksum", InitialDirectory = startPathCkecked && Directory.Exists(startPath) ? startPath : null}; |
| 46 | + if (openFileDialog.ShowDialog() == true) |
| 47 | + { |
| 48 | + selectedFiles = openFileDialog; |
| 49 | + |
| 50 | + foreach(string filePath in openFileDialog.FileNames) |
| 51 | + { |
| 52 | + ListOfFiles.Add(new FileParameters() { DlLink = startDlLinkCkecked && Uri.IsWellFormedUriString(startDlLink, UriKind.Absolute) && File.Exists(filePath) && startPathCkecked && Directory.Exists(startPath) ? new Uri(new Uri(startDlLink, UriKind.Absolute), Path.GetRelativePath(startPath + '/', filePath), false).AbsoluteUri : null, FileName = Path.GetFileName(filePath), Path = startPathCkecked ? Directory.Exists(startPath) ? Path.GetRelativePath(startPath, filePath).Replace(@"//", @"/").Replace(@"\\", @"\").Replace(@"\", @"/") : null : Directory.Exists(startPath) ? Path.GetFullPath(filePath) : null, Sha256 = File.Exists(filePath) ? FileSHA256(filePath) : null }); ; |
| 53 | + } |
| 54 | + |
| 55 | + textBoxResult.Text = JsonConvert.SerializeObject(ListOfFiles, Formatting.Indented); |
| 56 | + MessageBox.Show(JsonConvert.SerializeObject(ListOfFiles, Formatting.Indented)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static string FileSHA256(string filePath) |
| 61 | + { |
| 62 | + using System.Security.Cryptography.SHA256 hashAlgorithm = System.Security.Cryptography.SHA256.Create(); |
| 63 | + using (FileStream stream = System.IO.File.OpenRead(filePath)) |
| 64 | + { |
| 65 | + return BitConverter.ToString(hashAlgorithm.ComputeHash(stream)).Replace("-", "").ToUpper(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public static List<FileParameters> ListOfFiles { get; set; } = new(); |
| 71 | + public partial class FileParameters |
| 72 | + { |
| 73 | + [JsonProperty("dlLink", NullValueHandling = NullValueHandling.Ignore)] |
| 74 | + public string DlLink { get; set; } |
| 75 | + |
| 76 | + [JsonProperty("path", NullValueHandling = NullValueHandling.Ignore)] |
| 77 | + public string Path { get; set; } |
| 78 | + |
| 79 | + [JsonProperty("fileName", NullValueHandling = NullValueHandling.Ignore)] |
| 80 | + public string FileName { get; set; } |
| 81 | + |
| 82 | + [JsonProperty("sha256", NullValueHandling = NullValueHandling.Ignore)] |
| 83 | + public string Sha256 { get; set; } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments