private void btnExtract_Click(object sender, EventArgs e) { string password = FormFunc.ShowDialog("Enter password", "Extract files", true); if (password == config.PasswordText) { string source = null; string target = null; folderBrowserDialog1.Description = "Select source folder..."; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { source = folderBrowserDialog1.SelectedPath; folderBrowserDialog1.Description = "Select target folder..."; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { target = folderBrowserDialog1.SelectedPath; } } if (source != null && target != null) { //ProcHandler.ExtractFiles(source, target); ExtractWorker.RunWorkerAsync(source + ";" + target); } } else { MsgManager.Show("Wrong password!", "Extract files", MessageBoxIcon.Error); } }
public static void DeleteFolder(string folder) { try { if (!Directory.Exists(folder)) { throw new FileNotFoundException(string.Format("Can not delete '{0}', folder does not exist.", folder)); } Directory.Delete(folder, false); } catch (Exception exp) { MsgManager.Show(exp.Message, "Folder delete error", System.Windows.Forms.MessageBoxIcon.Error); } }
private void button2_Click(object sender, EventArgs e) { string source = null; string target = null; folderBrowserDialog1.Description = "Select source folder..."; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { source = folderBrowserDialog1.SelectedPath; folderBrowserDialog1.Description = "Select target folder..."; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { target = folderBrowserDialog1.SelectedPath; } } if (source != null && target != null) { foreach (FolderList fl in FolderList.list) { if (Regex.IsMatch(fl.Source + @"\\", Regex.Escape(source + @"\\"))) { MsgManager.Show(string.Format("Source folder is already added would conflict with another folder. {0} > {1}", source, fl.Source), "Add folder", MessageBoxIcon.Error); source = null; break; } if (Regex.IsMatch(fl.Target + @"\\", Regex.Escape(target + @"\\"))) { MsgManager.Show(string.Format("Target folder is already added would conflict with another folder. {0} > {1}", target, fl.Target), "Add folder", MessageBoxIcon.Error); target = null; break; } } } if (source != null && target != null) { FolderList fl = new FolderList(); fl.Source = source; fl.Target = target; FolderList.list.Add(fl); PopulateFolderList(); } }
public static void DeleteFile(string file) { try { if (!File.Exists(file)) { throw new FileNotFoundException(string.Format("Can not delete '{0}', file does not exist.", file)); } File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } catch (Exception exp) { MsgManager.Show(exp.Message, "File delete error", System.Windows.Forms.MessageBoxIcon.Error); } }
private void Form1_Load(object sender, EventArgs e) { Debug.Print("Application started"); if (minimixeWindow) { this.WindowState = FormWindowState.Minimized; } FileDetail.FileName = currentFolder + "\\FileDetails.xml"; if (System.IO.File.Exists(FileDetail.FileName)) { XML.DeSerializeList <FileDetail>(FileDetail.FileName, FileDetail.list); tslStatus.Text = string.Format("FileDetails loaded {0} files.", FileDetail.list.Count); } else { tslStatus.Text = "FileDetails not found!"; } FolderList.FileName = currentFolder + "\\FolderList.xml"; if (System.IO.File.Exists(FolderList.FileName)) { XML.DeSerializeList <FolderList>(FolderList.FileName, FolderList.list); PopulateFolderList(); } Config.FileName = currentFolder + "\\Config.xml"; if (System.IO.File.Exists(Config.FileName)) { XML.DeSerializeConfig(Config.FileName); config.Load(); } if (!File.Exists(config.Executable7zip)) { MsgManager.Show("No 7-Zip folder defined, select it in the settings tab!", "7-Zip folder", MessageBoxIcon.Warning); } FolderWatch.Init(); if (startSyncNow && !SyncWorker.IsBusy) { SyncWorker.RunWorkerAsync("Command-line argument"); } }
private void btnUpdateFolder_Click(object sender, EventArgs e) { if (lstFolders.SelectedItems.Count > 0) { if (System.IO.Directory.Exists(txtFolderSource.Text) && System.IO.Directory.Exists(txtFolderTarget.Text)) { FolderList fl = FolderList.list.First(f => f.Source == lstFolders.SelectedItems[0].Text); fl.Source = txtFolderSource.Text; fl.Target = txtFolderTarget.Text; PopulateFolderList(); } else { MsgManager.Show("Source and target folder must exist.", "Update folder", MessageBoxIcon.Error); } } }
private void btn7zip_Click(object sender, EventArgs e) { folderBrowserDialog1.Description = "Select 7-Zip folder..."; folderBrowserDialog1.SelectedPath = config.Path7zip; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { string exe7zip = folderBrowserDialog1.SelectedPath + @"\7z.exe"; if (File.Exists(exe7zip)) { config.Path7zip = folderBrowserDialog1.SelectedPath; CtrlInvoke.SetStatus(statusStrip1, tslStatus, "Found: " + exe7zip); } else { MsgManager.Show("7z.exe not found in folder.", "7-Zip folder", MessageBoxIcon.Error); } } }
//private void HandleRename() //{ // foreach (var item in filesRenamed) // { // FileDetail fd = FileDetail.ReturnObject(item.Key); // string oCompressed = fd.CompressedName; // fd.OriginalName = item.Value; // System.IO.File.Move(oCompressed, fd.CompressedName); // Debug.Print("Renamed file {0} to {1}", oCompressed, fd.CompressedName); // Log(System.IO.Path.GetDirectoryName(oCompressed), System.IO.Path.GetFileName(oCompressed), string.Format("File renamed to: {0}", fd.CompressedName), LogColor.Amber); // } // filesRenamed.Clear(); //} void SyncWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { CtrlInvoke.ButtonEnable(btnSyncStop, true); CtrlInvoke.ButtonEnable(btnSyncStart, false); CtrlInvoke.ButtonEnable(btnExtract, false); if (this.WindowState == FormWindowState.Minimized) { notifyIcon1.ShowBalloonTip(5000, "NoPeekCloud", "Synchronizing...", ToolTipIcon.Info); } ListViewItem runLogItem = new ListViewItem(new[] { DateTime.Now.ToString("dd.MM.yy"), DateTime.Now.ToString("HH:mm:ss"), (string)e.Argument }); CtrlInvoke.SetLog(lstRunLog, runLogItem); List <FileDetail> tempFileDetail = new List <FileDetail>(); CtrlInvoke.SetText(txtLastRun, DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString()); Stopwatch stopWatch = new Stopwatch(); if (string.IsNullOrEmpty(config.PasswordText)) { MsgManager.Show("Password is empty, files will not be encrypted.", "Empty password", MessageBoxIcon.Warning); } changeTimer = -1; int totalCount = 0; try { stopWatch.Start(); CtrlInvoke.SetStatus(statusStrip1, tslStatus, "Renaming files..."); //HandleRename(); CtrlInvoke.SetStatus(statusStrip1, tslStatus, "Finding files..."); int x = 0; foreach (FolderList fl in FolderList.list) { System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(fl.Source); Regex re = new Regex(@"(\.tmp|^~|^thumbs\.db$|^desktop\.ini$)", RegexOptions.IgnoreCase); fl.SourceFiles.AddRange(d.EnumerateFiles("*.*", System.IO.SearchOption.AllDirectories).Where(f => !re.IsMatch(System.IO.Path.GetFileName(f.FullName))).ToList()); System.IO.DirectoryInfo dt = new System.IO.DirectoryInfo(fl.Target); fl.CompressedFiles.AddRange(dt.GetFiles("*.*", System.IO.SearchOption.AllDirectories).Select(path => path.FullName).ToList()); totalCount += fl.SourceFiles.Count; } RemainingTime rt = new RemainingTime(totalCount, 250); CtrlInvoke.SetText(txtFileTotal, totalCount.ToString()); CtrlInvoke.SetStatus(statusStrip1, tslStatus, "Synchronizing..."); foreach (FolderList fl in FolderList.list) { Debug.Print("Working on folder: {0}", fl.Source); Debug.Indent(); foreach (var file in fl.SourceFiles) { Debug.Print("Found file: {0}", file.FullName); Debug.Indent(); CtrlInvoke.SetText(txtFileCurrent, x.ToString()); CtrlInvoke.SetText(txtProgressFolder, file.DirectoryName); CtrlInvoke.SetText(txtProgressFile, file.Name); FileDetail fd = new FileDetail(); fd.OriginalName = file.FullName; fd.OriginalPath = fl.Source; fd.OriginalHash = Crypto.GetSHA512Hash(file.FullName); fd.OriginalTime = file.LastWriteTimeUtc; fd.OriginalSize = file.Length; //fd.CompressedName = fd.CompressedNamePath; if (System.IO.File.Exists(fd.CompressedName)) { System.IO.FileInfo cFi = new System.IO.FileInfo(fd.CompressedName); //fd.CompressedHash = Crypto.GetSHA512Hash(cFi.FullName); fd.CompressedTime = cFi.LastWriteTimeUtc; fd.CompressedSize = cFi.Length; } fl.CompressedFiles.Remove(fd.CompressedName); if (FileDetail.ObjectExists(file.FullName) > 0) { Debug.Print("File exists, checking details..."); FileDetail fd2 = FileDetail.ReturnObject(file.FullName); if (fd.OriginalHash == fd2.OriginalHash && fd.OriginalSize == fd2.OriginalSize && fd.OriginalTime == fd2.OriginalTime) { Debug.Print("Files are equal, check if compressed file is correct..."); //if (fd.CompressedHash == fd2.CompressedHash && fd.CompressedSize == fd2.CompressedSize && fd.CompressedTime == fd2.CompressedTime && !config.ForceCompressedCreation) if (fd.CompressedSize == fd2.CompressedSize && fd.CompressedTime == fd2.CompressedTime && !config.ForceCompressedCreation) { Debug.Print("Compressed file checks out, do nothing..."); } else { if (!config.ForceCompressedCreation) { Debug.Print("Compressed file does not match, recreate!"); //if (fd.CompressedHash != fd2.CompressedHash) Debug.Print("Hash is different: {0} <> {1}", fd.CompressedHash, fd2.CompressedHash); if (fd.CompressedSize != fd2.CompressedSize) { Debug.Print("Size is different: {0} <> {1}", fd.CompressedSize, fd2.CompressedSize); } if (fd.CompressedTime != fd2.CompressedTime) { Debug.Print("Time is different: {0} <> {1}", fd.CompressedTime, fd2.CompressedTime); } logWriter.FileLog(file.DirectoryName, file.Name, "Compressed file does not match, recreate!", LogColor.Blue); } else { Debug.Print("Forcing creation of compressed file!"); logWriter.FileLog(file.DirectoryName, file.Name, "Forcing creation of compressed file!", LogColor.Blue); } ProcHandler.Run7zip(fd, fl); } } else { Debug.Print("Files differ, do something!"); if (fd.OriginalHash != fd2.OriginalHash) { Debug.Print("Hash is different: {0} <> {1}", fd.OriginalHash, fd2.OriginalHash); } if (fd.OriginalSize != fd2.OriginalSize) { Debug.Print("Size is different: {0} <> {1}", fd.OriginalSize, fd2.OriginalSize); } if (fd.OriginalTime != fd2.OriginalTime) { Debug.Print("Time is different: {0} <> {1}", fd.OriginalTime, fd2.OriginalTime); } logWriter.FileLog(file.DirectoryName, file.Name, "Files differ, do something!", LogColor.Amber); ProcHandler.Run7zip(fd, fl); } } else { Debug.Print("File not seen before, encrypt and add to list."); logWriter.FileLog(file.DirectoryName, file.Name, "File not seen before, encrypt and add.", LogColor.Green); //FileDetail.list.Add(fd); ProcHandler.Run7zip(fd, fl); } tempFileDetail.Add(fd); x++; SyncWorker.ReportProgress(Function.ReturnPercent(x, totalCount)); if (SyncWorker.CancellationPending) { Debug.Unindent(); break; } Debug.Unindent(); CtrlInvoke.SetText(txtRemainingTime, rt.Calculate(stopWatch.ElapsedMilliseconds, x)); } if (!SyncWorker.CancellationPending) { foreach (string deleteFile in fl.CompressedFiles) { Debug.Print("Compressed file {0} not in use, delete.", deleteFile); logWriter.FileLog(System.IO.Path.GetDirectoryName(deleteFile), System.IO.Path.GetFileName(deleteFile), "Compressed file not in use, delete.", LogColor.Red); FileHandler.DeleteFile(deleteFile); } foreach (var directory in System.IO.Directory.GetDirectories(fl.Target, "*", SearchOption.AllDirectories)) { if (System.IO.Directory.GetFiles(directory).Length == 0 && System.IO.Directory.GetDirectories(directory).Length == 0) { Debug.Print("Deleting empty folder {0}.", directory); logWriter.FileLog(directory, string.Empty, "Deleting empty folder", LogColor.Red); FileHandler.DeleteFolder(directory); } } } else { Debug.Unindent(); break; } Debug.Unindent(); } if (!SyncWorker.CancellationPending) { List <FileDetail> deleteResult = FileDetail.list.Except(tempFileDetail).ToList(); foreach (FileDetail fd in deleteResult) { Debug.Print("FileDetail entry {0} points to no file that does not exist. Delete.", fd.OriginalName); logWriter.FileLog(System.IO.Path.GetDirectoryName(fd.OriginalName), System.IO.Path.GetFileName(fd.OriginalName), "FileDetail entry not in use, delete.", LogColor.Red); } } } catch (Exception exp) { MsgManager.LaunchExceptionReporter(exp); } finally { try { if (System.IO.File.Exists(FileDetail.FileName)) { System.IO.File.Copy(FileDetail.FileName, currentFolder + "\\FileDetails.bak", true); } if (!SyncWorker.CancellationPending) { XML.SerializeList <FileDetail>(FileDetail.FileName, tempFileDetail); FileDetail.list = tempFileDetail; } } catch (Exception exp2) { MsgManager.LaunchExceptionReporter(exp2); } finally { stopWatch.Stop(); CtrlInvoke.SetText(txtSyncStopWatch, stopWatch.Elapsed.ToString()); CtrlInvoke.SetText(txtSyncNumberOfFiles, totalCount.ToString()); foreach (FolderList fl in FolderList.list) { fl.SourceFiles.Clear(); fl.CompressedFiles.Clear(); } } } }
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception exp = (Exception)e.ExceptionObject; MsgManager.LaunchExceptionReporter(exp); }
void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { Exception exp = (Exception)e.Exception; MsgManager.LaunchExceptionReporter(exp); }