NTP Analyzer  0.8.2
Analyze the operation of time servers
ShellCommand.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.IO;
24 using Ntp.Common.Log;
25 
26 namespace Ntp.Common.IO
27 {
28  public sealed class ShellCommand
29  {
30  public ShellCommand(string command, string arguments, LogBase log, string message)
31  {
32  this.command = command;
33  this.arguments = arguments;
34  this.log = log;
35  ErrorMessage = message;
36  }
37 
38  private readonly string arguments;
39  private readonly string command;
40 
41  private readonly LogBase log;
42  public static string WorkingDirectory { get; set; }
43 
44  public string ErrorMessage { get; set; }
45 
46  public StreamReader Execute()
47  {
48  var proc = new global::System.Diagnostics.Process
49  {
50  EnableRaisingEvents = false,
51  StartInfo =
52  {
53  UseShellExecute = false,
54  RedirectStandardOutput = true,
55  RedirectStandardError = true,
56  WorkingDirectory = WorkingDirectory,
57  FileName = command,
58  Arguments = $" {arguments}"
59  }
60  };
61 
62  string error;
63  log.ShellCommandExecuting(command, arguments);
64 
65  try
66  {
67  proc.Start();
68  error = proc.StandardError.ReadToEnd().Replace(Environment.NewLine, " ");
69  proc.WaitForExit();
70  }
71  catch (Exception e)
72  {
73  log.WriteLine(e.Message, Severity.Warn);
74  throw;
75  }
76 
77  if (error == string.Empty)
78  return proc.StandardOutput;
79 
80  log.ShellCommandError(ErrorMessage, error);
81  return null;
82  }
83  }
84 }
ShellCommand(string command, string arguments, LogBase log, string message)
Definition: ShellCommand.cs:30
readonly LogBase log
Definition: ShellCommand.cs:41
var e
Definition: bootstrap.min.js:6
readonly string arguments
Definition: ShellCommand.cs:38
readonly string command
Definition: ShellCommand.cs:39