void CopyFiles() { CurrentStatus = 1; _block.WaitOne(); foreach (var item in SourceItemsCollection.Where(c => c.Item3 == 0)) { OldBytes = 0; if (this.OPType == OperationType.Copy) { if (item.Item3 == 1) { if (!Directory.Exists(item.Item2)) { try { Directory.CreateDirectory(item.Item2); } catch (UnauthorizedAccessException) { WindowsAPI.SendMessage(MessageReceiverHandle, WM_FOERROR, IntPtr.Zero, IntPtr.Zero); Environment.Exit(5); } } } else { try { ProcessItems(item.Item1, item.Item2); } catch (Exception) { WindowsAPI.SendMessage(MessageReceiverHandle, WM_FOERROR, IntPtr.Zero, IntPtr.Zero); Environment.Exit(5); } }; } if (this.OPType == OperationType.Move) { if (item.Item3 == 0) { try { ProcessItems(item.Item1, item.Item2); } catch (Exception) { WindowsAPI.SendMessage(MessageReceiverHandle, WM_FOERROR, IntPtr.Zero, IntPtr.Zero); Environment.Exit(5); } } foreach (var dir in this.SourceItemsCollection.Select(c => ShellObject.FromParsingName(c.Item1)).ToArray().Where(c => c.IsFolder)) { DeleteFolderRecursive(new DirectoryInfo(dir.ParsingName)); } GC.WaitForPendingFinalizers(); GC.Collect(); } if (this.OPType == OperationType.Delete) { foreach (var entry in this.SourceItemsCollection) { _block.WaitOne(); try { if (!Directory.Exists(entry.Item1) || (Path.GetExtension(entry.Item1).ToLowerInvariant() == ".zip")) { var itemInfo = new FileInfo(entry.Item1); if (itemInfo.IsReadOnly) { File.SetAttributes(entry.Item1, FileAttributes.Normal); } if (this.DeleteToRB) { Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(entry.Item1, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing); } else { File.Delete(entry.Item1); } byte[] data = System.Text.Encoding.Unicode.GetBytes(String.Format("{0}|{1}|{2}|{3}", 1, 0, totaltransfered, entry.Item1)); WindowsAPI.SendStringMessage(MessageReceiverHandle, data, 0, data.Length); } else { if (this.DeleteToRB) { RecycleBin.SendSilent(entry.Item1); } else { DeleteAllFilesFromDir(new DirectoryInfo(entry.Item1)); DeleteFolderRecursive(new DirectoryInfo(entry.Item1)); } } } catch (Exception) { WindowsAPI.SendMessage(MessageReceiverHandle, WM_FOERROR, IntPtr.Zero, IntPtr.Zero); Environment.Exit(5); } } } } }
protected override void WndProc(ref Message m) { if (m.Msg == WindowsAPI.WM_COPYDATA) { byte[] b = new Byte[Marshal.ReadInt32(m.LParam, IntPtr.Size)]; IntPtr dataPtr = Marshal.ReadIntPtr(m.LParam, IntPtr.Size * 2); Marshal.Copy(dataPtr, b, 0, b.Length); string newMessage = System.Text.Encoding.Unicode.GetString(b); if (newMessage.StartsWith("END FO INIT|COPY")) { this.OPType = OperationType.Copy; } if (newMessage.StartsWith("END FO INIT|MOVE")) { this.OPType = OperationType.Move; } if (newMessage.StartsWith("END FO INIT|DELETE")) { this.OPType = OperationType.Delete; } if (newMessage.Contains("DeleteTORB")) { this.DeleteToRB = true; } if (newMessage.StartsWith("INPUT|")) { var parts = newMessage.Replace("INPUT|", "").Split(Char.Parse("|")); SourceItemsCollection.Add(new Tuple <string, string, int>(parts[0].Trim(), parts[1].Trim(), Convert.ToInt32(parts[2].Trim()))); } if (newMessage.StartsWith("END FO INIT")) { _block.Set(); CopyThread = new Thread(new ThreadStart(CopyFiles)); CopyThread.IsBackground = false; CopyThread.Start(); CopyThread.Join(1); } if (newMessage.StartsWith("COMMAND|")) { var realMessage = newMessage.Replace("COMMAND|", String.Empty); switch (realMessage) { case "STOP": this.Cancel = true; _block.Set(); _block2.Set(); break; case "PAUSE": _block.Reset(); break; case "CONTINUE": _block.Set(); break; case "CLOSE": Close(); break; default: break; } } } base.WndProc(ref m); }