Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Threading;
  6. using WebClientFtp.Properties;
  7.  
  8. namespace WebClientFtp.FTP
  9. {
  10. public class UploadFiles
  11. {
  12. private SortedList<string, string> ArgList = new SortedList<string, string>(4);
  13. private WebClient _webClient;
  14.  
  15. public UploadFiles(string[] args)
  16. {
  17. string key = null;
  18.  
  19. foreach (string arg in args)
  20. {
  21. if (arg[0] == '-')
  22. {
  23. key = arg.ToLower();
  24. }
  25. else if (key != null)
  26. {
  27. if (!ArgList.ContainsKey(key))
  28. {
  29. ArgList.Add(key, arg);
  30. }
  31. }
  32. }
  33. if (!ArgList.ContainsKey("-source") || !ArgList.ContainsKey("-ftpdest"))
  34. {
  35. Console.WriteLine("Usage: WebClientFtp -source <path-to-local-folder> -ftpdest <uri-ftp-directory>");
  36. Console.WriteLine(" [ -user <userName> -pwd <password> ]");
  37. Console.WriteLine(" -source and -ftpdest are required arguments");
  38. Console.ReadKey(true);
  39. Environment.Exit(1);
  40. }
  41. _webClient = new WebClient();
  42. }
  43.  
  44. public void CopyFolderFiles()
  45. {
  46. string sPath = ArgList["-source"];
  47.  
  48. if (Directory.Exists(sPath))
  49. {
  50. //get all files in path
  51. string[] files = Directory.GetFiles(sPath, "*.*", SearchOption.TopDirectoryOnly);
  52. //if user and password exist, set credentials
  53. if (ArgList.ContainsKey("-user") && ArgList.ContainsKey("-pwd"))
  54. {
  55. _webClient.Credentials = new NetworkCredential(ArgList["-user"], ArgList["-pwd"]);
  56. }
  57. bool UploadCompleted;
  58. int wait = Settings.Default.WaitInterval;
  59. string ftpDest = ArgList["-ftpdest"];
  60. //loop through files in folder and upload
  61. foreach (string file in files)
  62. {
  63. do
  64. {
  65. UploadCompleted = ftpUpFile(file, ftpDest);
  66. if (!UploadCompleted)
  67. {
  68. Thread.Sleep(wait);
  69. wait += 1000; //wait an extra second after each failed attempt
  70. }
  71. } while (!UploadCompleted);
  72. }
  73. }
  74. }
  75.  
  76. private bool ftpUpFile(string filePath, string ftpUri)
  77. {
  78. try
  79. {
  80. Console.WriteLine("Uploading file: " + filePath + "\nTo: " + ftpUri);
  81.  
  82. //create full uri path
  83. string file = ftpUri + "/" + Path.GetFileName(filePath);
  84.  
  85. //ftp the file to Uri path via the ftp STOR command
  86. // (ignoring the the Byte[] array return since it is always empty in this case)
  87. _webClient.UploadFile(file, filePath);
  88. Console.WriteLine("Upload complete.");
  89. return true;
  90. }
  91. catch (Exception ex)
  92. {
  93. //WebException is frequenty thrown for this condition:
  94. // "An error occurred while uploading the file"
  95. Console.WriteLine(ex.Message);
  96. return false;
  97. }
  98. }
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement