CaUtil_openssl_1.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CAUtilLib
  9. {
  10. public class ExecShellCmdResult
  11. {
  12. public string StdOutPut { get; set; } = string.Empty;
  13. public string StdErrOutPut { get; set; } = string.Empty;
  14. public int? ExitCode { get; set; } = null;
  15. public bool IsSuccess => ExitCode == 0;
  16. public static implicit operator bool(ExecShellCmdResult result) => result.IsSuccess;
  17. }
  18. public partial class CaUtil_openssl
  19. {
  20. public async Task<bool> MergeFile(string outFile, string inFile1, string inFile2)
  21. {
  22. outFile = Path.Combine(path, outFile);
  23. inFile1 = Path.Combine(path, inFile1);
  24. inFile2 = Path.Combine(path, inFile2);
  25. try
  26. {
  27. if (!File.Exists(inFile1) ||
  28. !File.Exists(inFile2))
  29. {
  30. return false;
  31. }
  32. var oStream = File.OpenWrite(outFile);
  33. await File.OpenRead(inFile1).CopyToAsync(oStream);
  34. await File.OpenRead(inFile2).CopyToAsync(oStream);
  35. oStream.Close();
  36. return true;
  37. }
  38. catch (Exception e)
  39. {
  40. logger.LogCritical(e.Message);
  41. }
  42. return false;
  43. }
  44. private async Task<string> GetOpenSSLRandSn()
  45. {
  46. var result = await ExecShellCmd("openssl", "rand -hex 8");
  47. return result ? result.StdOutPut.Trim() : "" ;
  48. }
  49. public async Task<ExecShellCmdResult> ExecShellCmd(string fileName, string arguments)
  50. {
  51. var toReturn = new ExecShellCmdResult();
  52. Process process = new Process();
  53. process.EnableRaisingEvents = true;
  54. //process.OutputDataReceived += Process_OutputDataReceived;
  55. //process.ErrorDataReceived += Process_ErrorDataReceived;
  56. // set the process start info
  57. process.StartInfo.FileName = fileName; // specify the command to run
  58. process.StartInfo.Arguments = arguments; // specify the arguments
  59. // set additional process start info as necessary
  60. process.StartInfo.WorkingDirectory = this.path;
  61. process.StartInfo.UseShellExecute = false;
  62. process.StartInfo.RedirectStandardInput = true;
  63. process.StartInfo.RedirectStandardOutput = true;
  64. process.StartInfo.RedirectStandardError = true;
  65. // start the process
  66. var startResult = process.Start();
  67. // wait for the process to exit
  68. await process.WaitForExitAsync();
  69. //process.BeginOutputReadLine();
  70. //process.BeginErrorReadLine();
  71. toReturn.StdOutPut = await process.StandardOutput.ReadToEndAsync();
  72. toReturn.StdErrOutPut = await process.StandardError.ReadToEndAsync();
  73. toReturn.ExitCode = process.ExitCode;
  74. Console.WriteLine(toReturn.StdOutPut);
  75. Console.WriteLine(toReturn.StdErrOutPut);
  76. return toReturn;
  77. //return process.ExitCode == 0;
  78. //void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
  79. //{
  80. // toReturn.StdOutPut += e.Data;
  81. // logger.LogTrace(e.Data);
  82. // Console.WriteLine(e.Data);
  83. //}
  84. //void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  85. //{
  86. // toReturn.StdErrOutPut += e.Data;
  87. // logger.LogTrace(e.Data);
  88. // Console.WriteLine(e.Data);
  89. //}
  90. }
  91. }
  92. }