Differences

This shows you the differences between the selected revisions of the page.

2021-03-25 2021-03-25
reducing nesting (martin) modernizing c# download code (martin)
Line 21: Line 21:
        {         {
            // Setup session options             // Setup session options
-            SessionOptions sessionOptions = new SessionOptions+            var sessionOptions = new SessionOptions
            {             {
                Protocol = Protocol.Sftp,                 Protocol = Protocol.Sftp,
Line 34: Line 34:
            const int batches = 3;             const int batches = 3;
-            DateTime started = DateTime.Now;+            var started = DateTime.Now;
            int count = 0;             int count = 0;
-            Int64 bytes = 0;+            long bytes = 0;
-            using (Session session = new Session())+            using (var session = new Session())
            {             {
                Console.WriteLine("Connecting...");                 Console.WriteLine("Connecting...");
Line 44: Line 44:
                Console.WriteLine("Starting files enumeration...");                 Console.WriteLine("Starting files enumeration...");
 +                var opts = WinSCP.EnumerationOptions.AllDirectories;
                IEnumerable<RemoteFileInfo> files =                 IEnumerable<RemoteFileInfo> files =
-                    session.EnumerateRemoteFiles( +                    session.EnumerateRemoteFiles(remotePath, null, opts);
-························remotePath, null, EnumerationOptions.AllDirectories);+
                IEnumerator<RemoteFileInfo> filesEnumerator = files.GetEnumerator();                 IEnumerator<RemoteFileInfo> filesEnumerator = files.GetEnumerator();
-                List<Task> tasks = new List<Task>();+                var tasks = new List<Task>();
                for (int i = 1; i <= batches; i++)                 for (int i = 1; i <= batches; i++)
Line 55: Line 55:
                    int no = i;                     int no = i;
-                    Task task = new Task(() =>+                    var task = new Task(() =>
                    {                     {
-                        using (Session downloadSession = new Session())+                        using (var downloadSession = new Session())
                        {                         {
-                            Console.WriteLine("Starting download {0}...", no);+                            Console.WriteLine($"Starting download {no}...");
                            downloadSession.Open(sessionOptions);                             downloadSession.Open(sessionOptions);
Line 82: Line 82:
                                        remoteFilePath, remotePath, localPath);                                         remoteFilePath, remotePath, localPath);
                                Console.WriteLine(                                 Console.WriteLine(
-                                    "Downloading {0} to {1} in {2}..."+                                    $"Downloading {remoteFilePath} to {localFilePath} in {no}..."); 
-                                    remoteFilePath, localFilePath, no); +                                string localFileDir = Path.GetDirectoryName(localFilePath); 
-                                Directory.CreateDirectory( +                                Directory.CreateDirectory(localFileDir); 
- ···································Path.GetDirectoryName(localFilePath)); + ·······························downloadSession.GetFileToDirectory(remoteFilePath, localFileDir);
-                                downloadSession.GetFiles+
- ···································RemotePath.EscapeFileMask(remoteFilePath), +
-                                   localFilePath).Check();+
                            }                             }
-                            Console.WriteLine("Download {0} done", no);+                            Console.WriteLine($"Download {no} done");
                        }                         }
- 
                    });                     });
Line 106: Line 102:
            Console.WriteLine("Done");             Console.WriteLine("Done");
-            DateTime ended = DateTime.Now; +            var ended = DateTime.Now; 
-            Console.WriteLine("Took {0}", (ended - started)); +            Console.WriteLine($"Took {ended - started}"); 
-            Console.WriteLine("Downloaded {0} files, totaling {1:N0} bytes", count, bytes);+            Console.WriteLine($"Downloaded {count} files, totaling {bytes:N0} bytes");
            return 0;             return 0;
Line 114: Line 110:
        catch (Exception e)         catch (Exception e)
        {         {
-            Console.WriteLine("Error: {0}", e);+            Console.WriteLine($"Error: {e}");
            return 1;             return 1;
        }         }

Last modified: by martin