我需要使用 FileSystemWatcher 將操作從 dir1 同步到 dir2。當我在 dir1 中創建一個新檔案時,相同的檔案必須在 dir2 中,如果我洗掉/重命名/移動,也應該在 dir2 中。
當我在 dir1 中創建檔案時,它也在 dir2 中創建,但是當我嘗試在 dir1 中重命名檔案時,我得到這個例外: 未處理的例外。System.IO.IOException:該行程無法訪問檔案“C:\Users\artio\Desktop\FASassignment\root\dir1\g.txt”,因為它正被另一個行程使用。 當我用谷歌搜索時,可能會出現此錯誤,因為流未關閉,但我不知道我是否使用了一個。另外,當我在其中創建一個子檔案夾和一個檔案時,我收到一個錯誤,即找不到路徑并且在 dir2 中它沒有創建,你能幫我解決這兩個錯誤嗎?提前致謝!
程式:
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 = one rror;
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}";
string destinationPath = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
var name = e.Name;
var fullPath = e.FullPath;
string destination = Path.Combine(destinationPath, name);
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)
{
var oldFullPath = e.OldFullPath;
var fullPath = e?.FullPath;
Console.WriteLine($"Renamed:");
Console.WriteLine($" Old: {e.OldFullPath}");
Console.WriteLine($" New: {e.FullPath}");
string destinationPath = @"C:\Users\artio\Desktop\FASassignment\root\dir2";
string destination = Path.Combine(destinationPath, fullPath);
File.Copy(fullPath, destination, true);
File.Delete(oldFullPath);
}
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);
}
}
}
uj5u.com熱心網友回復:
你的問題是這一行:
string destination = Path.Combine(destinationPath, fullPath);
除錯它,您會發現這并不會導致您的想法。您正在嘗試合并兩個完整路徑。
相反,使用這個:
private 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 oldDestinationPath = Path.Combine(Destination, oldFileName);
var newDestinationPath = Path.Combine(Destination, newFileName);
var info = new FileInfo(oldDestinationPath);
info.MoveTo(newDestinationPath);
}
注意我沒有使用這個File類。眾所周知,這是有問題的。
另外,我建議使用某種行程日志并重試,以便恢復。否則,這可能會留下很多更改,因為檔案可能會保留它們,這可能會導致您的操作失敗。
更新:
關于您關于其他檔案的其他問題,看起來您正在嘗試在創建它的行程釋放它的句柄之前訪問該檔案,因此您可以嘗試引入一秒或其他東西的小延遲:
private void OnCreated(object sender, FileSystemEventArgs e)
{
var name = e.Name;
var fullPath = e.FullPath;
string destination = Path.Combine(Destination, name);
Console.WriteLine($"Copy to: {destination}");
Thread.Sleep(1000); //Intentional delay
File.Copy(fullPath, destination, true);
Console.WriteLine("File copied successfully.");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437088.html
上一篇:一次在多個子目錄中創建占位符檔案
