private void Watcher_Renamed(object sender, RenamedEventArgs e) { ThreadInteropUtils.OpeMainFormControl(() => { this.richTextBox1.Text += "Rename folder or file was happened..." + Environment.NewLine; }, this); //SyncProcess(); string oldname = destRootPath + "\\" + e.OldName; FileOpHelper.DeleteDirOrFile(oldname); FileOpHelper.CopyDirectory(sourceRootPath, destRootPath, true); //string destPath = destRootPath + "\\" + e.Name; //if (!Directory.Exists(destPath) && FileOpHelper.isFile(destPath) == false) // Directory.CreateDirectory(destPath); //if (!File.Exists(destPath) && FileOpHelper.isFile(destPath)) //{ // FileInfo flinfo = new FileInfo(e.FullPath); // flinfo.CopyTo(destPath, true); //} ThreadInteropUtils.OpeMainFormControl(() => { this.richTextBox1.Text += "Synchronizing file completed..." + Environment.NewLine + Environment.NewLine; }, this); ThreadInteropUtils.OpeMainFormControl(() => { getSpecifiedPathDirsList(sourceRootPath, this.treeView1); getSpecifiedPathDirsList(destRootPath, this.treeView2); }, this); }
private void Form1_Load(object sender, EventArgs e) { sourceRootPath = this.textBox1.Text.Trim(); destRootPath = this.textBox2.Text.Trim(); myDocsPath = sourceRootPath.Substring(0, sourceRootPath.LastIndexOf(@"\") + 1); tempDir = Path.Combine(myDocsPath, "Cache"); trashDir = Path.Combine(myDocsPath, "Trash"); PrepareDir(sourceRootPath); PrepareDir(destRootPath); PrepareDir(tempDir); PrepareDir(trashDir); this.richTextBox1.Text = "The prepared folder or file has been created" + Environment.NewLine; getSpecifiedPathDirsList(sourceRootPath, this.treeView1); getSpecifiedPathDirsList(destRootPath, this.treeView2); Thread t = new Thread(() => { while (1 == 1) { if (fileQueue.Count > 0) { h++; //Console.WriteLine("process files:" + fileQueue.Count); string destFilePath = null; //fileQueue.TryDequeue(out destFilePath); var fileInfo = new MyFileInfo(); fileQueue.TryDequeue(out fileInfo); destFilePath = fileInfo.DestinationPath; FileStream fs = null; switch (fileInfo.ChnageType) { case WatcherChangeTypes.Created: try { // 真实创建文件方法 if (Path.GetExtension(destFilePath) != string.Empty) { fs = File.Create(destFilePath); fs.Dispose(); fs.Close(); } else { if (!Directory.Exists(destFilePath)) { Directory.CreateDirectory(destFilePath); } } } //catch (Exception ex) //{ // Console.WriteLine(ex.Message); // goto aa; //} finally { //if (h >= 3000) //{ // h = 0; // Thread.Sleep(1000); //} if (fileQueue.Count == 0) { ThreadInteropUtils.OpeMainFormControl(() => { getSpecifiedPathDirsList(sourceRootPath, this.treeView1); getSpecifiedPathDirsList(destRootPath, this.treeView2); }, this); } } break; case WatcherChangeTypes.Changed: while (!FileOpHelper.FileIsReady(fileInfo.SourcePath)) { continue; } File.Copy(fileInfo.SourcePath, destFilePath, true); // 复制文件总是无法将内容复制成功 break; } } else { Thread.Sleep(1000); } } }); t.IsBackground = true; t.Start(); Thread t2 = new Thread(() => { while (1 == 1) { if (delFileQueue.Count > 0) { k++; //Console.WriteLine("process del files:" + delFileQueue.Count); string destFilePath = null; delFileQueue.TryDequeue(out destFilePath); bb: try { // 真正删除文件方法 //if (Directory.Exists(destFilePath)) //{ // Directory.Delete(destFilePath); //} //if (File.Exists(destFilePath)) //{ // File.Delete(destFilePath); //} FileOpHelper.DeleteDirOrFile(destFilePath); } catch (Exception ex) { Console.WriteLine(ex.Message); goto bb; } finally { //if (k >= 3000) //{ // k = 0; // Thread.Sleep(1000); //} if (delFileQueue.Count == 0) { ThreadInteropUtils.OpeMainFormControl(() => { getSpecifiedPathDirsList(sourceRootPath, this.treeView1); getSpecifiedPathDirsList(destRootPath, this.treeView2); }, this); } } } else { Thread.Sleep(1000); } } }); t2.IsBackground = true; t2.Start(); }