private void SyncFileOperate(string sourceRootPath, string destRootPath, string tempDir, string trashDir) { FileSyncScopeFilter filter = new FileSyncScopeFilter(); filter.FileNameExcludes.Add("*.metadata"); FileSyncOptions options = FileSyncOptions.None; //DetectChanges DetectChangesOnFileSystemReplica(sourceRootPath, filter, options, sourceRootPath, "filesync.metadata", tempDir, trashDir); DetectChangesOnFileSystemReplica(destRootPath, filter, options, destRootPath, "filesync.metadata", tempDir, trashDir); try { ThreadInteropUtils.OpeMainFormControl(() => { this.richTextBox1.Text += "Start synchronizing..." + Environment.NewLine; }, this); //SyncChanges Both Ways SyncOperationStatistics syncOperationStatistics = null; SyncFileUtils.SyncFileSystemReplicasOneWay(sourceRootPath, destRootPath, filter, options, sourceRootPath, "filesync.metadata", destRootPath, "filesync.metadata", tempDir, trashDir, ref syncOperationStatistics); //ThreadInteropUtils.OpeMainFormControl(() => //{ // this.richTextBox1.Text += "Synchronizing file upload changes... " + syncOperationStatistics.UploadChangesApplied.ToString() + Environment.NewLine; //}, this); } catch (Exception e) { throw e; } }
private void ProcessRefresh(object sender, ElapsedEventArgs args) { try { //Console.WriteLine("Processing queue, " + fileQueue.Count + " files created:"); //rwlock.EnterReadLock(); //foreach (string fls in filePaths) //{ // Console.WriteLine(fls); // FileInfo flinfo = new FileInfo(fls); // flinfo.CopyTo(destRootPath + "\\" + flinfo.Name, false); //} //filePaths.Clear(); //string fls; //while (fileQueue.TryDequeue(out fls)) //{ // Console.WriteLine(fls); // FileOpHelper.DeleteDirOrFile(fls); //} ThreadInteropUtils.OpeMainFormControl(() => { getSpecifiedPathDirsList(sourceRootPath, this.treeView1); getSpecifiedPathDirsList(destRootPath, this.treeView2); }, this); } finally { //rwlock.ExitReadLock(); } }
private void SyncProcess() { try { // 同步文件 SyncFileOperate(sourceRootPath, destRootPath, tempDir, trashDir); ThreadInteropUtils.OpeMainFormControl(() => { this.richTextBox1.Text += "Synchronizing file completed..." + Environment.NewLine + Environment.NewLine; }, this); } catch (Exception ex) { ThreadInteropUtils.OpeMainFormControl(() => { this.richTextBox1.Text += "Synchronizing file error..." + " Error Detail:" + ex.Message + Environment.NewLine + Environment.NewLine; }, this); } }
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(); }