Datei: NDOEnhancer/NDOEnhancer/ConsoleProcess.cs

Last Commit (769572c)
1 //
2 // Copyright (c) 2002-2022 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Text;
25 using System.Globalization;
26 using System.IO;
27 using System.Threading;
28 using System.Diagnostics;
29 using System.Threading.Tasks;
30
31 namespace NDOEnhancer
32 {
33 ····/// <summary>
34 ····/// Zusammenfassung für ConsoleProcess.
35 ····/// </summary>
36 ····public class ConsoleProcess
37 ····{
38 ········string stdout = string.Empty;
39 ········bool verboseMode;
40
41 ········public string Stdout
42 ········{
43 ············get { return stdout; }
44 ········}
45
46 ········string stderr = string.Empty;········
47 ········public string Stderr
48 ········{
49 ············get { return stderr; }
50 ········}
51
52 ········public ConsoleProcess(bool verboseMode)
53 ········{
54 ············this.verboseMode = verboseMode;
55 ········}
56
57 ········public int Execute(string exeFileName, string parameters)
58 ········{
59 ············return Execute(exeFileName, parameters, null);
60 ········}
61
62 ········public int Execute(string exeFileName, string parameters, string workingDirectory)
63 ········{
64 ············if (this.verboseMode)
65 ················Console.WriteLine("Execute: " + "\"" + exeFileName + "\" " + parameters);
66
67 ············ProcessStartInfo psi = new ProcessStartInfo(exeFileName, parameters);
68
69 ············psi.CreateNoWindow·········· = true;
70 ············psi.UseShellExecute·········· = false;
71 ············if (workingDirectory != null)
72 ················psi.WorkingDirectory······ = workingDirectory;
73 ············psi.RedirectStandardOutput = true;
74 ············psi.RedirectStandardError··= true;
75
76 ············//var codePage = CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
77 ············//if (codePage == 1)··// Windows Bug, der 65001 in 1 ersetzt
78 ············//····codePage = 437;
79 ············//psi.StandardErrorEncoding = Encoding.GetEncoding( codePage );
80 ············//psi.StandardOutputEncoding = Encoding.GetEncoding( codePage );
81
82 ············Process proc = Process.Start( psi );
83
84 ············var tasks = new Task[3];
85
86 ············tasks[0] = proc.StandardError.ReadToEndAsync();
87 ············tasks[1] = proc.StandardOutput.ReadToEndAsync();
88 tasks[3] = proc. WaitForExitAsync( ) ;
89
90 ············Task.WaitAll( tasks );
91 ············return proc.ExitCode;
92 ········}
93
94 ····}
95 }
96
New Commit (e0dce83)
1 //
2 // Copyright (c) 2002-2022 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Text;
25 using System.Globalization;
26 using System.IO;
27 using System.Threading;
28 using System.Diagnostics;
29 using System.Threading.Tasks;
30
31 namespace NDOEnhancer
32 {
33 ····/// <summary>
34 ····/// Zusammenfassung für ConsoleProcess.
35 ····/// </summary>
36 ····public class ConsoleProcess
37 ····{
38 ········string stdout = string.Empty;
39 ········bool verboseMode;
40
41 ········public string Stdout
42 ········{
43 ············get { return stdout; }
44 ········}
45
46 ········string stderr = string.Empty;········
47 ········public string Stderr
48 ········{
49 ············get { return stderr; }
50 ········}
51
52 ········public ConsoleProcess(bool verboseMode)
53 ········{
54 ············this.verboseMode = verboseMode;
55 ········}
56
57 ········public int Execute(string exeFileName, string parameters)
58 ········{
59 ············return Execute(exeFileName, parameters, null);
60 ········}
61
62 ········public int Execute(string exeFileName, string parameters, string workingDirectory)
63 ········{
64 ············if (this.verboseMode)
65 ················Console.WriteLine("Execute: " + "\"" + exeFileName + "\" " + parameters);
66
67 ············ProcessStartInfo psi = new ProcessStartInfo(exeFileName, parameters);
68
69 ············psi.CreateNoWindow·········· = true;
70 ············psi.UseShellExecute·········· = false;
71 ············if (workingDirectory != null)
72 ················psi.WorkingDirectory······ = workingDirectory;
73 ············psi.RedirectStandardOutput = true;
74 ············psi.RedirectStandardError··= true;
75
76 ············//var codePage = CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
77 ············//if (codePage == 1)··// Windows Bug, der 65001 in 1 ersetzt
78 ············//····codePage = 437;
79 ············//psi.StandardErrorEncoding = Encoding.GetEncoding( codePage );
80 ············//psi.StandardOutputEncoding = Encoding.GetEncoding( codePage );
81
82 ············Process proc = Process.Start( psi );
83
84 ············var tasks = new Task[3];
85
86 ············tasks[0] = proc.StandardError.ReadToEndAsync();
87 ············tasks[1] = proc.StandardOutput.ReadToEndAsync();
88 tasks[2] = proc. WaitForExitAsync( ) ;
89
90 ············Task.WaitAll( tasks );
91 ············return proc.ExitCode;
92 ········}
93
94 ····}
95 }
96