Advertisement
Guest User

WinSCP upload

a guest
Mar 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using WinSCP;
  6.  
  7. class Example
  8. {
  9.     static int Main()
  10.     {
  11.         try
  12.         {
  13.             // Setup session options
  14.             SessionOptions sessionOptions = new SessionOptions
  15.             {
  16.                 Protocol = Protocol.Sftp,
  17.                 HostName = "example.com",
  18.                 UserName = "user",
  19.                 Password = "password",
  20.                 SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx..."
  21.             };
  22.  
  23.             const string localPath = @"C:\local\path";
  24.             const string remotePath = "/remote/path";
  25.             const int batches = 3;
  26.  
  27.             DateTime started = DateTime.Now;
  28.             int count = 0;
  29.             Int64 bytes = 0;
  30.  
  31.             Console.WriteLine("Starting files enumeration...");
  32.             IEnumerable<string> files =
  33.                 Directory.EnumerateFiles(localPath, "*.*", SearchOption.AllDirectories);
  34.             IEnumerator<string> filesEnumerator = files.GetEnumerator();
  35.  
  36.             List<Task> tasks = new List<Task>();
  37.  
  38.             HashSet<string> existingRemotePaths = new HashSet<string>();
  39.  
  40.             for (int i = 1; i <= batches; i++)
  41.             {
  42.                 int no = i;
  43.  
  44.                 Task task = new Task(() =>
  45.                 {
  46.                     using (Session uploadSession = new Session())
  47.                     {
  48.                         while (true)
  49.                         {
  50.                             string localFilePath;
  51.                             lock (filesEnumerator)
  52.                             {
  53.                                 if (!filesEnumerator.MoveNext())
  54.                                 {
  55.                                     break;
  56.                                 }
  57.  
  58.                                 localFilePath = filesEnumerator.Current;
  59.                                 bytes += new FileInfo(localFilePath).Length;
  60.                                 count++;
  61.                             }
  62.  
  63.                             if (!uploadSession.Opened)
  64.                             {
  65.                                 Console.WriteLine("Starting upload {0}...", no);
  66.                                 uploadSession.Open(sessionOptions);
  67.                             }
  68.  
  69.                             string remoteFilePath =
  70.                                 RemotePath.TranslateLocalPathToRemote(
  71.                                     localFilePath, localPath, remotePath);
  72.                             Console.WriteLine(
  73.                                 "Uploading {0} to {1} in {2}...",
  74.                                 localFilePath, remoteFilePath, no);
  75.  
  76.                             string path =
  77.                                 remoteFilePath.Substring(0, remoteFilePath.LastIndexOf('/'));
  78.                             string current = "";
  79.  
  80.                             if (path.Substring(0, 1) == "/")
  81.                             {
  82.                                 path = path.Substring(1);
  83.                             }
  84.  
  85.                             while (!string.IsNullOrEmpty(path))
  86.                             {
  87.                                 int p = path.IndexOf('/');
  88.                                 current += '/';
  89.                                 if (p >= 0)
  90.                                 {
  91.                                     current += path.Substring(0, p);
  92.                                     path = path.Substring(p + 1);
  93.                                 }
  94.                                 else
  95.                                 {
  96.                                     current += path;
  97.                                     path = "";
  98.                                 }
  99.  
  100.                                 lock (existingRemotePaths)
  101.                                 {
  102.                                     if (!existingRemotePaths.Contains(current)) // optimization
  103.                                     {
  104.                                         if (!uploadSession.FileExists(current))
  105.                                         {
  106.                                             Console.WriteLine("Creating {0}...", current);
  107.                                             uploadSession.CreateDirectory(current);
  108.                                         }
  109.                                         existingRemotePaths.Add(current);
  110.                                     }
  111.                                 }
  112.                             }
  113.  
  114.                             uploadSession.PutFiles(
  115.                                 localFilePath, RemotePath.EscapeFileMask(remoteFilePath)).
  116.                                 Check();
  117.                         }
  118.  
  119.                         if (uploadSession.Opened)
  120.                         {
  121.                             Console.WriteLine("Upload {0} done", no);
  122.                         }
  123.                         else
  124.                         {
  125.                             Console.WriteLine("Upload {0} had nothing to do", no);
  126.                         }
  127.                     }
  128.  
  129.                 });
  130.  
  131.                 tasks.Add(task);
  132.                 task.Start();
  133.             }
  134.  
  135.             Console.WriteLine("Waiting for uploads to complete...");
  136.             Task.WaitAll(tasks.ToArray());
  137.  
  138.             Console.WriteLine("Done");
  139.  
  140.             DateTime ended = DateTime.Now;
  141.             Console.WriteLine("Took {0}", (ended - started));
  142.             Console.WriteLine("Uploaded {0} files, totaling {1:N0} bytes", count, bytes);
  143.  
  144.             return 0;
  145.         }
  146.         catch (Exception e)
  147.         {
  148.             Console.WriteLine("Error: {0}", e);
  149.             return 1;
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement