chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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.Diagnostics;
using System.IO;
using System.Text;
 
namespace OpenTap.Package
{
    internal static class ProgramHelper
    {
        private static OpenTap.TraceSource log = Log.CreateSource("Program");
 
        internal static int RunProgram(string program, string arguments)
        {
            StringBuilder stdOut = new StringBuilder();
            StringBuilder stdErr = new StringBuilder();
 
            stdOut.AppendLine($"Running '{program}' with '{arguments}'");
 
            ProcessStartInfo pi = new ProcessStartInfo(program, arguments);
            pi.CreateNoWindow = true;
            pi.UseShellExecute = false;
            pi.RedirectStandardError = true;
            pi.RedirectStandardOutput = true;
 
            var p = Process.Start(pi);
 
            p.ErrorDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) stdErr.AppendLine(e.Data); };
            p.OutputDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) stdOut.AppendLine(e.Data); };
 
            p.BeginErrorReadLine();
            p.BeginOutputReadLine();
 
            p.WaitForExit();
 
            try
            {
                if (p.ExitCode != 0)
                {
                    var ex = new Exception("Error during run program: " + p.ExitCode);
 
                    ex.Data["StdOut"] = stdOut.ToString();
                    ex.Data["StdErr"] = stdErr.ToString();
 
                    throw ex;
                }
 
                return p.ExitCode;
            }
            finally
            {
                p.Close();
            }
        }
 
        internal static void FileCopy(string source, string destination)
        {
            const int RetryTimeoutMS = 10;
 
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    File.Copy(source, destination, true);
                    break;
                }
                catch
                {
                    System.Threading.Thread.Sleep(RetryTimeoutMS);
                    if (i == 9)
                        throw;
                }
            }
 
        }
    }
}