1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Diagnostics ;
34using System . IO ;
5+ using GeneralUpdate . Common . FileBasic ;
46
57namespace GeneralUpdate . Bowl . Strategys ;
68
79public abstract class AbstractStrategy : IStrategy
810{
911 protected MonitorParameter _parameter ;
10-
11- private readonly IReadOnlyList < string > _sensitiveCharacter = new List < string >
12- {
13- "Exit" ,
14- "exit" ,
15- "EXIT"
16- } ;
12+ protected List < string > OutputList = new ( ) ;
13+
14+ public void SetParameter ( MonitorParameter parameter ) => _parameter = parameter ;
1715
1816 public virtual void Launch ( )
1917 {
20- Backup ( ) ;
21- Startup ( _parameter . ProcessNameOrId , _parameter . InnerArguments ) ;
18+ Startup ( _parameter . InnerApp , _parameter . InnerArguments ) ;
2219 }
2320
2421 private void Startup ( string appName , string arguments )
2522 {
23+ if ( Directory . Exists ( _parameter . FailDirectory ) )
24+ {
25+ Directory . Delete ( _parameter . FailDirectory , true ) ;
26+ }
27+ Directory . CreateDirectory ( _parameter . FailDirectory ) ;
28+
2629 var startInfo = new ProcessStartInfo
2730 {
2831 FileName = appName ,
2932 Arguments = arguments ,
3033 RedirectStandardOutput = true ,
34+ RedirectStandardError = true ,
3135 UseShellExecute = false ,
3236 CreateNoWindow = true
3337 } ;
@@ -36,71 +40,15 @@ private void Startup(string appName, string arguments)
3640 process . OutputDataReceived += OutputHandler ;
3741 process . ErrorDataReceived += OutputHandler ;
3842 process . Start ( ) ;
39- process . StandardOutput . ReadToEnd ( ) ;
40- process . WaitForExit ( ) ;
43+ process . BeginOutputReadLine ( ) ;
44+ process . BeginErrorReadLine ( ) ;
45+ process . WaitForExit ( 1000 * 10 ) ;
4146 }
4247
4348 private void OutputHandler ( object sendingProcess , DataReceivedEventArgs outLine )
4449 {
4550 var data = outLine . Data ;
4651 if ( ! string . IsNullOrEmpty ( data ) )
47- {
48- foreach ( var sensitive in _sensitiveCharacter )
49- {
50- if ( data . Contains ( sensitive ) ) {
51- Restore ( ) ;
52- Process . Start ( _parameter . ProcessNameOrId , _parameter . Arguments ) ;
53- break ;
54- }
55- }
56- }
52+ OutputList . Add ( data ) ;
5753 }
58-
59- private void Backup ( )
60- {
61- var backupPath = _parameter . Target ;
62- var sourcePath = _parameter . Source ;
63-
64- if ( Directory . Exists ( backupPath ) )
65- {
66- Directory . Delete ( backupPath , true ) ;
67- }
68-
69- Directory . CreateDirectory ( backupPath ) ;
70-
71- foreach ( string dirPath in Directory . GetDirectories ( sourcePath , "*" , SearchOption . AllDirectories ) )
72- {
73- Directory . CreateDirectory ( dirPath . Replace ( sourcePath , backupPath ) ) ;
74- }
75-
76- foreach ( string newPath in Directory . GetFiles ( sourcePath , "*.*" , SearchOption . AllDirectories ) )
77- {
78- File . Copy ( newPath , newPath . Replace ( sourcePath , backupPath ) , true ) ;
79- }
80- }
81-
82- private void Restore ( )
83- {
84- var restorePath = _parameter . Target ;
85- var backupPath = _parameter . Source ;
86-
87- if ( Directory . Exists ( restorePath ) )
88- {
89- Directory . Delete ( restorePath , true ) ;
90- }
91-
92- Directory . CreateDirectory ( restorePath ) ;
93-
94- foreach ( string dirPath in Directory . GetDirectories ( backupPath , "*" , SearchOption . AllDirectories ) )
95- {
96- Directory . CreateDirectory ( dirPath . Replace ( backupPath , restorePath ) ) ;
97- }
98-
99- foreach ( string newPath in Directory . GetFiles ( backupPath , "*.*" , SearchOption . AllDirectories ) )
100- {
101- File . Copy ( newPath , newPath . Replace ( backupPath , restorePath ) , true ) ;
102- }
103- }
104-
105- public void SetParameter ( MonitorParameter parameter ) => _parameter = parameter ;
10654}
0 commit comments