123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CAUtilLib
- {
- public class ExecShellCmdResult
- {
- public string StdOutPut { get; set; } = string.Empty;
- public string StdErrOutPut { get; set; } = string.Empty;
- public int? ExitCode { get; set; } = null;
- public bool IsSuccess => ExitCode == 0;
- public static implicit operator bool(ExecShellCmdResult result) => result.IsSuccess;
- }
- public partial class CaUtil_openssl
- {
- public async Task<bool> MergeFile(string outFile, string inFile1, string inFile2)
- {
- outFile = Path.Combine(path, outFile);
- inFile1 = Path.Combine(path, inFile1);
- inFile2 = Path.Combine(path, inFile2);
- try
- {
- if (!File.Exists(inFile1) ||
- !File.Exists(inFile2))
- {
- return false;
- }
- var oStream = File.OpenWrite(outFile);
- await File.OpenRead(inFile1).CopyToAsync(oStream);
- await File.OpenRead(inFile2).CopyToAsync(oStream);
- oStream.Close();
- return true;
- }
- catch (Exception e)
- {
- logger.LogCritical(e.Message);
- }
- return false;
- }
- private async Task<string> GetOpenSSLRandSn()
- {
- var result = await ExecShellCmd("openssl", "rand -hex 8");
- return result ? result.StdOutPut.Trim() : "" ;
- }
- public async Task<ExecShellCmdResult> ExecShellCmd(string fileName, string arguments)
- {
- var toReturn = new ExecShellCmdResult();
- Process process = new Process();
- process.EnableRaisingEvents = true;
-
-
-
- process.StartInfo.FileName = fileName;
- process.StartInfo.Arguments = arguments;
-
- process.StartInfo.WorkingDirectory = this.path;
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
-
- var startResult = process.Start();
-
- await process.WaitForExitAsync();
-
-
- toReturn.StdOutPut = await process.StandardOutput.ReadToEndAsync();
- toReturn.StdErrOutPut = await process.StandardError.ReadToEndAsync();
- toReturn.ExitCode = process.ExitCode;
- Console.WriteLine(toReturn.StdOutPut);
- Console.WriteLine(toReturn.StdErrOutPut);
- return toReturn;
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
- }
|