我有一个本地文件夹D:\test
,我想要与远程服务器文件夹/home/test
同步。下面的代码在正常情况下可以正常工作。但问题是,当我在运行同步时从本地文件夹处理一个或多个文件时,它会抛出一个错误并停止同步。
堆栈跟踪:
Exception thrown: 'System.IO.IOException' in mscorlib.dll Upload of D:\test\~$New Microsoft PowerPoint Presentation.pptx failed:
WinSCP.SessionRemoteException: Can't open file 'D:\test\~$New Microsoft PowerPoint Presentation.pptx'.
System Error. Code: 32.
The process cannot access the file because it is being used by another process
Permissions of kept with their defaults
Timestamp of kept with its default (current time)
Exception thrown: 'WinSCP.SessionRemoteException' in WinSCPnet.dll
The thread 0x26c0 has exited with code 0 (0x0).
Error: WinSCP.SessionRemoteException: Can't open file 'D:\test\~$New Microsoft PowerPoint Presentation.pptx'.
System Error. Code: 32.
The process cannot access the file because it is being used by another process
at WinSCP.OperationResultBase.Check()```
我的实施:
try
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "hostName",
UserName = "root",
Password = "p@ssword",
PortNumber = 22,
SshHostKeyPolicy = SshHostKeyPolicy.GiveUpSecurityAndAcceptAny
};
using (Session session = new Session())
{
session.Open(sessionOptions);
session.Timeout = new TimeSpan(1, 0, 0);
session.FileTransferred += FileTransferred;
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Automatic;
transferOptions.AddRawSettings("ReplaceInvalidChars", "0");
transferOptions.ResumeSupport.State = TransferResumeSupportState.On;
SynchronizationResult synchronizationResult;
synchronizationResult = session.SynchronizeDirectories(SynchronizationMode.Remote, @"D:\test\", "/home/test/", true, false, SynchronizationCriteria.Time, transferOptions);
synchronizationResult.Check();
}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex);
}
private static void FileTransferred(object sender, TransferEventArgs e)
{
if (e.Error == null)
{
Console.WriteLine("Upload of {0} succeeded", e.FileName);
}
else
{
Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error);
}
if (e.Chmod != null)
{
if (e.Chmod.Error == null)
{
Console.WriteLine("Permissions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions);
}
else
{
Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error);
}
}
else
{
Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination);
}
if (e.Touch != null)
{
if (e.Touch.Error == null)
{
Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime);
}
else
{
Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error);
}
}
else
{
Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination);
}
}
是否有任何方法来捕捉特定的错误并继续同步?
发布于 2022-09-18 06:41:06
可以使WinSCP .NET程序集跳过处理事件无法打开的所有文件和文件夹。
session.QueryReceived +=
(sender, args) => {
Console.WriteLine($"Error: {args.Message}");
args.Continue();
};
基于文章递归下载具有自定义错误处理的目录树和类似的PowerShell问题使用WinSCP在PowerShell中将整个驱动器上载到服务器,跳过无法读取的所有文件和文件夹的代码。
另一种选择是使用TransferOptions.FileMask
显式排除传输中存在问题的文件和文件夹。
https://stackoverflow.com/questions/73760512
复制相似问题