[英]How to use FileSystemWatcher for synchronizing two folders action's
所以,我有一個任務:
實時同步2個目錄的內容
給定 2 個目錄的路徑,監聽目錄 1 的變化並與目錄 2 同步操作。這意味着當我在目錄 1 中創建一個新文件時,相同的文件應該在目錄 2 中,當我刪除或修改文件時也會發生同樣的情況.Dir1 可能有嵌套文件夾。
我有這個起點:
public class Program2
{
public static void Main(string[] args)
{
string sourcePath = @"C:\Users\artio\Desktop\FASassignment\root\dir1";
string destinationPath = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
var source = new DirectoryInfo(sourcePath);
var destination = new DirectoryInfo(destinationPath);
using var sourceWatcher = new FileSystemWatcher(sourcePath);
sourceWatcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
sourceWatcher.Changed += OnChanged;
sourceWatcher.Created += OnCreated;
sourceWatcher.Deleted += OnDeleted;
sourceWatcher.Renamed += OnRenamed;
sourceWatcher.Error += OnError;
sourceWatcher.Filter = "*.txt";
sourceWatcher.IncludeSubdirectories = true;
sourceWatcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Console.WriteLine($"Changed: {e.FullPath}");
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Created: {e.FullPath}";
Console.WriteLine(value);
}
private static void OnDeleted(object sender, FileSystemEventArgs e) =>
Console.WriteLine($"Deleted: {e.FullPath}");
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"Renamed:");
Console.WriteLine($" Old: {e.OldFullPath}");
Console.WriteLine($" New: {e.FullPath}");
}
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());
private static void PrintException(Exception? ex)
{
if (ex != null)
{
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine("Stacktrace:");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}
}
在onCreated
事件中,我添加了這一行:
File.Copy(e.FullPath, 目的地);
如果它在主目錄中,它會創建文件,但如果它在嵌套文件夾中,則會引發錯誤。 然后我嘗試在事件中添加此代碼:
FileInfo destFile = new FileInfo(destinationFolder + srcFile.FullName.Replace(sourceFolder, ""));
if (!Directory.Exists(destFile.DirectoryName) && createFolders)
{
Directory.CreateDirectory(destFile.DirectoryName);
}
//Check if src file was modified and modify the destination file
if (srcFile.LastWriteTime > destFile.LastWriteTime || !destFile.Exists)
{
File.Copy(srcFile.FullName, destFile.FullName, true);
}
另外,沒有用。 我應該怎么做才能完成這項任務? 我正在學習 c#,我不知道所有的細節,在此先感謝!
我已經完成了這個任務,但在這個程序中仍然存在一個小問題,當我嘗試在 dir1 中創建一個子文件夾時,它給出了未經授權的異常,我無法訪問子文件夾路徑,我認為我沒有以好的方式組合它們,但我不知道如何修復它。現在,該程序在 dir2 中鏡像了 dir1 中的操作,沒有子文件夾。
public class Program2
{
public static void Main(string[] args)
{
string sourcePath = @"C:\Users\artio\Desktop\FASassignment\root\dir1";
string destinationPath = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
var source = new DirectoryInfo(sourcePath);
var destination = new DirectoryInfo(destinationPath);
using var sourceWatcher = new FileSystemWatcher(sourcePath);
sourceWatcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
sourceWatcher.Changed += OnChanged;
sourceWatcher.Created += OnCreated;
sourceWatcher.Deleted += OnDeleted;
sourceWatcher.Renamed += OnRenamed;
sourceWatcher.Error += OnError;
sourceWatcher.IncludeSubdirectories = true;
sourceWatcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
string destinationPath = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
var fullPath = e.FullPath;
var name = e?.Name;
string destination = Path.Combine(destinationPath, name);
File.Copy(fullPath, destination, true);
Console.WriteLine($"Changed: {e.FullPath}");
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Created: {e.FullPath}";
var Destination = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
var name = e.Name;
var fullPath = e.FullPath;
string destination = Path.Combine(Destination, name);
Console.WriteLine($"Copy to: {destination}");
Thread.Sleep(1000);
File.Copy(fullPath, destination, true);
Console.WriteLine(value);
}
private static void OnDeleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"Deleted: {e.FullPath}");
var name = e.Name;
string destinationPath = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
string destination = Path.Combine(destinationPath, name);
File.Delete(destination);
}
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"Renamed:");
Console.WriteLine($" Old: {e.OldFullPath}");
Console.WriteLine($" New: {e.FullPath}");
var oldFileName = Path.GetFileName(e.OldFullPath);
var newFileName = Path.GetFileName(e.FullPath);
var Destination = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
var oldDestinationPath = Path.Combine(Destination, oldFileName);
var newDestinationPath = Path.Combine(Destination, newFileName);
var info = new FileInfo(oldDestinationPath);
info.MoveTo(newDestinationPath);
}
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());
private static void PrintException(Exception? ex)
{
if (ex != null)
{
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine("Stacktrace:");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.