首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果另一个进程正在使用一个或多个文件,则WinSCP Session.SynchronizeDirectories()将引发错误

如果另一个进程正在使用一个或多个文件,则WinSCP Session.SynchronizeDirectories()将引发错误
EN

Stack Overflow用户
提问于 2022-09-18 06:14:50
回答 1查看 125关注 0票数 1

我有一个本地文件夹D:\test,我想要与远程服务器文件夹/home/test同步。下面的代码在正常情况下可以正常工作。但问题是,当我在运行同步时从本地文件夹处理一个或多个文件时,它会抛出一个错误并停止同步。

堆栈跟踪:

代码语言:javascript
运行
复制
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()```

我的实施:

代码语言:javascript
运行
复制
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);
    }
}

是否有任何方法来捕捉特定的错误并继续同步?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-18 06:41:06

可以使WinSCP .NET程序集跳过处理事件无法打开的所有文件和文件夹。

代码语言:javascript
运行
复制
session.QueryReceived +=
    (sender, args) => {
        Console.WriteLine($"Error: {args.Message}");
        args.Continue();
    };

基于文章递归下载具有自定义错误处理的目录树和类似的PowerShell问题使用WinSCP在PowerShell中将整个驱动器上载到服务器,跳过无法读取的所有文件和文件夹的代码。

另一种选择是使用TransferOptions.FileMask显式排除传输中存在问题的文件和文件夹。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73760512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档