PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/UploadToShareFile/Program.cs

https://github.com/RickyLin/DotNetUtilities
C# | 130 lines | 105 code | 13 blank | 12 comment | 9 complexity | 5818fbd14610b4a410dfd0f2c062aa7e MD5 | raw file
  1. using ShareFileSampleCode;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Configuration;
  8. using System.IO;
  9. using System.Diagnostics;
  10. namespace UploadToShareFile
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. /*
  17. * args:
  18. * 0: src folder path, comma to separate multiple folders, do not leave white space around the comma
  19. * 1: the folder where the zip file resides.
  20. * 2: version file path
  21. * 3: sharefile folder path
  22. * 4: file name prefix
  23. */
  24. string[] srcDirPaths = null;
  25. string shareFileFolderId = null, fileNamePrefix = null, versionFilePath = null, zipFilePath = null;
  26. string userName = null, password = null;
  27. if (args.Length == 0)
  28. {
  29. Console.WriteLine(@" * args:
  30. * 0: src folder path, comma to separate multiple folders, do not leave white space around the comma
  31. * 1: the folder where the zip file resides.
  32. * 2: version file path
  33. * 3: sharefile folder path
  34. * 4: file name prefix");
  35. return;
  36. }
  37. Console.WriteLine("Get Configuration Information.");
  38. try
  39. {
  40. srcDirPaths = args[0].Split(',');
  41. zipFilePath = args[1];
  42. versionFilePath = args[2];
  43. fileNamePrefix = args[3];
  44. shareFileFolderId = args[4];
  45. userName = ConfigurationManager.AppSettings["UserName"];
  46. password = ConfigurationManager.AppSettings["Password"];
  47. }
  48. catch (Exception err)
  49. {
  50. Console.WriteLine("Failed to get config data. {0}", err.Message);
  51. Environment.ExitCode = -1;
  52. return;
  53. }
  54. Console.WriteLine("Check folders available.");
  55. foreach (string srcDirPath in srcDirPaths)
  56. {
  57. if (Directory.Exists(srcDirPath) == false)
  58. {
  59. Console.WriteLine("The folder '{0}' doesn't exist.", srcDirPath);
  60. Environment.ExitCode = -1;
  61. return;
  62. }
  63. }
  64. if (File.Exists(versionFilePath) == false)
  65. {
  66. Console.WriteLine("The file '{0}' doesn't exist.", versionFilePath);
  67. Environment.ExitCode = -1;
  68. return;
  69. }
  70. // create the zip file, using 7-zip to create archive
  71. string zipFileName = string.Format("{0}_{1}.zip", fileNamePrefix, File.ReadAllText(versionFilePath));
  72. string zipFilePathName = Path.Combine(zipFilePath, zipFileName);
  73. Console.WriteLine("Create file: {0}", zipFilePathName);
  74. Process zipProcess = new Process();
  75. string cmdParam = string.Format("a {0}", zipFilePathName);
  76. foreach (string srcDir in srcDirPaths)
  77. {
  78. cmdParam = cmdParam + " " + srcDir;
  79. }
  80. zipProcess.StartInfo = new ProcessStartInfo(@"C:\Program Files\7-Zip\7z.exe", cmdParam);
  81. zipProcess.Start();
  82. zipProcess.WaitForExit();
  83. if (zipProcess.ExitCode != 0)
  84. {
  85. Environment.ExitCode = zipProcess.ExitCode;
  86. return;
  87. }
  88. // upload the zip file to ShareFile
  89. Console.WriteLine("Upload zip file to ShareFile.");
  90. ShareFileSample sample = new ShareFileSample();
  91. //bool loginSuccess = sample.Authenticate("synvata", "sharefile.com", userName, password);
  92. bool loginSuccess = sample.Authenticate("synvata", "sharefile.com", "", "");
  93. if (!loginSuccess)
  94. {
  95. Console.WriteLine("Failed to Login ShareFile.");
  96. Environment.ExitCode = -1;
  97. return;
  98. }
  99. Dictionary<string, object> optionalParameters = new Dictionary<string, object>();
  100. optionalParameters.Add("folderid", shareFileFolderId); // the ReloviewsComplete folder
  101. optionalParameters.Add("overwrite", true);
  102. try
  103. {
  104. //zipFilePathName = @"C:\Users\li\Documents\GitHub\DotNetUtilities\UploadToShareFile\bin\Debug\ReloviewsComplete_6.0.4967.1340.zip";
  105. sample.FileUpload(zipFilePathName, optionalParameters);
  106. }
  107. catch (Exception err)
  108. {
  109. Console.WriteLine("Failed to upload. {0}", err.Message);
  110. Environment.ExitCode = -1;
  111. return;
  112. }
  113. Console.WriteLine("Done.");
  114. return;
  115. }
  116. }
  117. }