You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WPF/C#应用中用WinSCP实现本地到NAS的单向文件同步

Solution for Syncing Local Folders to NAS via WinSCP in WPF/C#

Hey Sebastian, let's get this sync setup sorted out for you! I see you're stuck on choosing the right protocol and fixing your WinSCP code to sync C:\Apps to your NAS share. Here's a step-by-step breakdown:

1. Pick the Correct Protocol

Your NAS uses a Windows file share (SMB/CIFS) (the \\server\share path format gives this away), not FTP. WinSCP fully supports SMB, so you'll need to switch your protocol setting to Protocol.Smb.

2. Fix SessionOptions Configuration

Your original code has a couple of small but critical issues with connection settings. Here's the corrected version:

var sessionOptions = new SessionOptions
{
    Protocol = Protocol.Smb,
    HostName = "192.168.1.20", // Remove the leading \\ here—just use the raw IP
    UserName = "Sebastian",
    Password = "MyNasPassword",
    RemotePath = "/Sebastian/Backup" // WinSCP uses forward slashes for SMB paths
};
  • HostName: Ditch the UNC prefix (\\)—WinSCP handles SMB paths separately via RemotePath
  • RemotePath: Maps directly to your NAS share \\192.168.1.20\Sebastian\Backup using forward slashes

3. Implement the One-Way Sync

With your session settings fixed, use WinSCP's SynchronizeDirectories method to run the one-way sync from your local folder to the NAS. Here's a complete, ready-to-use code example:

using WinSCP;

try
{
    var sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Smb,
        HostName = "192.168.1.20",
        UserName = "Sebastian",
        Password = "MyNasPassword",
        RemotePath = "/Sebastian/Backup"
    };

    using (Session session = new Session())
    {
        // Connect to the NAS share
        session.Open(sessionOptions);

        // Run one-way sync: local → remote
        SynchronizationResult syncResult = session.SynchronizeDirectories(
            SynchronizationMode.Remote, // Sync local changes to remote
            @"C:\Apps",
            sessionOptions.RemotePath,
            removeFiles: true // Set to false if you don't want to delete extra files on NAS
        );

        // Check for sync errors
        syncResult.Check();

        // Optional: Log sync actions (useful for debugging in your WPF app)
        foreach (SynchronizationEventArgs e in syncResult.Args)
        {
            System.Diagnostics.Debug.WriteLine($"Sync action: {e.FileOperation} | File: {e.FileName}");
        }
    }
}
catch (Exception ex)
{
    // Handle errors in your WPF app (e.g., show a message box to the user)
    System.Windows.MessageBox.Show($"Sync failed: {ex.Message}", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}
  • SynchronizationMode.Remote: Ensures changes flow only from your local C:\Apps to the NAS (one-way sync as you requested)
  • removeFiles: Set to true if you want to delete files/folders on the NAS that no longer exist locally; keep it false if you want to preserve those files.

4. Troubleshooting Tips

  • Double-check that your NAS has SMB/CIFS service enabled (most devices have this on by default, but verify in your NAS settings)
  • Confirm the user Sebastian has read/write permissions on the Sebastian/Backup share
  • Make sure no firewall is blocking SMB traffic (port 445) between your machine and the NAS
  • Test accessing the NAS share via File Explorer first to confirm your username/password works

内容的提问来源于stack exchange,提问作者Sebastian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:11:14