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.OutputDataReceived += Process_OutputDataReceived;
- //process.ErrorDataReceived += Process_ErrorDataReceived;
- // set the process start info
- process.StartInfo.FileName = fileName; // specify the command to run
- process.StartInfo.Arguments = arguments; // specify the arguments
- // set additional process start info as necessary
- process.StartInfo.WorkingDirectory = this.path;
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
- // start the process
- var startResult = process.Start();
- // wait for the process to exit
- await process.WaitForExitAsync();
- //process.BeginOutputReadLine();
- //process.BeginErrorReadLine();
- 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;
- //return process.ExitCode == 0;
- //void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
- //{
- // toReturn.StdOutPut += e.Data;
- // logger.LogTrace(e.Data);
- // Console.WriteLine(e.Data);
- //}
- //void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
- //{
- // toReturn.StdErrOutPut += e.Data;
- // logger.LogTrace(e.Data);
- // Console.WriteLine(e.Data);
- //}
- }
- }
- }
|