Blame view

Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools.cs 2.19 KB
37bf94ab   Schwirg László   command mode kial...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  using System;
  using System.IO;
  using System.Collections.Generic;
  using System.Linq;
  using System.Security.Principal;
  using System.Text;
  using System.Threading.Tasks;
  using System.Threading;
  
  using Microsoft.Web.Administration;
  using System.Management;
  using System.Diagnostics;
  
  using Vrh.XmlProcessing;
  using System.Xml.Linq;
  
  namespace Vrh.Log4Pro.MaintenanceConsole
  {
  	public static class Tools 
  	{
  		public static bool IsElevated
  		{
  			get
  			{
  				return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  			}
  		}
  		public static string ExecuteAndGetStdIo(string exepath, string args)
  		{
  			StringBuilder outputBuilder = new StringBuilder();
  			StringBuilder erroroutputBuilder = new StringBuilder();
  			ProcessStartInfo startInfo = new ProcessStartInfo() { };
  			using (Process exeProcess = new Process())
  			{
  				startInfo.CreateNoWindow = true;
  				startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  				startInfo.RedirectStandardOutput = true;
  				startInfo.RedirectStandardError= true;
  				startInfo.RedirectStandardInput = true;
  				startInfo.UseShellExecute = false;
  				startInfo.FileName = exepath;
  				startInfo.Verb= "runas";
  				startInfo.Arguments = args;
  
  				exeProcess.StartInfo = startInfo;
  				// enable raising events because Process does not raise events by default
  				exeProcess.EnableRaisingEvents = true;
  				exeProcess.OutputDataReceived += new DataReceivedEventHandler
  				(
  					delegate (object sender, DataReceivedEventArgs e)
  					{
  						// append the new data to the data already read-in
  						outputBuilder.Append(e.Data + "\r\n");
  					}
  				);
  				exeProcess.ErrorDataReceived += new DataReceivedEventHandler
  				(
  					delegate (object sender, DataReceivedEventArgs e)
  					{
  						// append the new data to the data already read-in
  						erroroutputBuilder.Append(e.Data + "\r\n");
  					}
  				);
  				// start the process
  				// then begin asynchronously reading the output
  				// then wait for the process to exit
  				// then cancel asynchronously reading the output
  				exeProcess.Start();
  				exeProcess.BeginOutputReadLine();
  				exeProcess.WaitForExit();
  				exeProcess.CancelOutputRead();
  				return outputBuilder.ToString();
  			}
  		}
  	}
  }