public static async Task <System.Collections.Generic.List <UniqueFile> > StartFolderHash(string folder) { System.Collections.Generic.List <UniqueFile> files = new System.Collections.Generic.List <UniqueFile>(); await Task.Run(() => { // TODO: Plug memory leak. // this function increases the memory usage every time it is run Log.WriteLine("Hashing " + folder); try { var folders = EnumerationIOUtility.GetAllFilesFromFolder(folder, true); foreach (var file in folders) { UniqueFile tentative = new UniqueFile(file); if (tentative.Hash != null) { files.Add(tentative); Log.WriteLine(string.Format("{0} *{1}", ByteArrayToString(tentative.Hash), tentative.Location)); } } } catch (Exception ex) { Log.WriteLine(ex.ToString()); } Log.WriteLine("Finished hashing " + folder); }); return(files); }
private static void Main(string[] args) { // Quit if already running https://stackoverflow.com/a/6392264 System.Threading.Mutex mutex = new System.Threading.Mutex(false, ProductName); var runBenchmark = false; var runHashTest = false; try { if (mutex.WaitOne(0, false)) { if (args != null && args.Length > 0) { for (var i = 0; i < args.Length; i++) { var s = args[i]; if (s == "--debug") { // Throw everything inside a new console window. This code works. AllocConsole(); System.IntPtr stdHandle = GetStdHandle(StdOutputHandle); Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(stdHandle, true); System.IO.FileStream fileStream = new System.IO.FileStream(safeFileHandle, System.IO.FileAccess.Write); System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(MyCodePage); System.IO.StreamWriter standardOutput = new System.IO.StreamWriter(fileStream, encoding) { AutoFlush = true }; System.Console.SetOut(standardOutput); } if (s == "--benchmark") { runBenchmark = true; } if (s == "--test") { runHashTest = true; } } } if (!runBenchmark) { // Show the system tray icon. System.Windows.Forms.Application.EnableVisualStyles(); System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed += OnTimedEvent; aTimer.Interval = 1000 * 7200; // 120 minutes aTimer.Enabled = true; // Put the icon in the system tray STrayIcon.Icon = Properties.Resources.HatSync; STrayIcon.Text = "HatSync"; STrayIcon.Visible = true; // Run once at startup IPUpdater.Init(); // Attach a context menu. MenuGenerator.RegenerateMenu(); if (runHashTest) { //============================================================== // TEST Hash a file. The value should always be the same // otherwise something went wrong with the implementation. // Create dummy file const string data = "Hello, world!"; const string name = "test.bin"; System.IO.File.WriteAllText(name, data); // Hash it UniqueFile result = new UniqueFile(name); Log.WriteLine("Hash of test file: " + result.GetHashAsString()); if (result.GetHashAsString() == "B5DA441CFE72AE042EF4D2B17742907F675DE4DA57462D4C3609C2E2ED755970") { Log.WriteLine("Test Sucessful"); } else { Log.WriteLine("Shit."); } if (System.IO.File.Exists(name)) { System.IO.File.Delete(name); } } System.Windows.Forms.Application.Run(); } #if USE_BENCHMARKDOTNET else { BenchmarkDotNet.Running.BenchmarkRunner.Run <Md5VsSha256VsBlake2>(); } #endif } } finally { mutex.Close(); mutex = null; } }