1616using System . Diagnostics . CodeAnalysis ;
1717using System . IO ;
1818using System . Linq ;
19- using System . ServiceProcess ;
2019using System . Text ;
21- using SeqCli . Forwarder . Util ;
20+ using Serilog ;
2221
23- namespace SeqCli . Forwarder . Util
22+ namespace SeqCli . Forwarder . Util ;
23+
24+ [ SuppressMessage ( "Interoperability" , "CA1416:Validate platform compatibility" ) ]
25+ public static class ServiceConfiguration
2426{
25- [ SuppressMessage ( "Interoperability" , "CA1416:Validate platform compatibility" ) ]
26- public static class ServiceConfiguration
27+ static bool GetServiceCommandLine ( string serviceName , [ MaybeNullWhen ( false ) ] out string path )
2728 {
28- public static bool GetServiceBinaryPath ( ServiceController controller , [ MaybeNullWhen ( false ) ] out string path )
29- {
30- var sc = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "sc.exe" ) ;
31-
32- var config = new StringBuilder ( ) ;
33- if ( 0 != CaptiveProcess . Run ( sc , "qc \" " + controller . ServiceName + "\" " , l => config . AppendLine ( l ) , Console . WriteLine ) )
34- {
35- Console . WriteLine ( "Could not query service path; ignoring." ) ;
36- path = null ;
37- return false ;
38- }
39-
40- var lines = config . ToString ( )
41- . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries )
42- . Select ( l => l . Trim ( ) ) ;
43-
44- var line = lines
45- . SingleOrDefault ( l => l . StartsWith ( "BINARY_PATH_NAME : " ) ) ;
46-
47- if ( line == null )
48- {
49- Console . WriteLine ( "No existing binary path could be determined." ) ;
50- path = null ;
51- return false ;
52- }
29+ var sc = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "sc.exe" ) ;
5330
54- path = line . Replace ( "BINARY_PATH_NAME : " , "" ) ;
55- return true ;
56- }
57-
58- static bool GetServiceCommandLine ( string serviceName , [ MaybeNullWhen ( false ) ] out string path )
31+ var config = new StringBuilder ( ) ;
32+ if ( 0 != CaptiveProcess . Run ( sc , "qc \" " + serviceName + "\" " , l => config . AppendLine ( l ) , Console . WriteLine ) )
5933 {
60- if ( serviceName == null ) throw new ArgumentNullException ( nameof ( serviceName ) ) ;
61-
62- var sc = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "sc.exe" ) ;
34+ Log . Debug ( "Could not query service path; ignoring." ) ;
35+ path = null ;
36+ return false ;
37+ }
6338
64- var config = new StringBuilder ( ) ;
65- if ( 0 != CaptiveProcess . Run ( sc , "qc \" " + serviceName + "\" " , l => config . AppendLine ( l ) , Console . WriteLine ) )
66- {
67- Console . WriteLine ( "Could not query service path; ignoring." ) ;
68- path = null ;
69- return false ;
70- }
39+ var lines = config . ToString ( )
40+ . Split ( [ Environment . NewLine ] , StringSplitOptions . RemoveEmptyEntries )
41+ . Select ( l => l . Trim ( ) ) ;
7142
72- var lines = config . ToString ( )
73- . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries )
74- . Select ( l => l . Trim ( ) ) ;
43+ var line = lines
44+ . SingleOrDefault ( l => l . StartsWith ( "BINARY_PATH_NAME : " ) ) ;
7545
76- var line = lines
77- . SingleOrDefault ( l => l . StartsWith ( "BINARY_PATH_NAME : " ) ) ;
46+ if ( line == null )
47+ {
48+ Log . Debug ( "No existing binary path could be determined." ) ;
49+ path = null ;
50+ return false ;
51+ }
7852
79- if ( line == null )
80- {
81- Console . WriteLine ( "No existing binary path could be determined." ) ;
82- path = null ;
83- return false ;
84- }
53+ path = line . Replace ( "BINARY_PATH_NAME : " , "" ) ;
54+ return true ;
55+ }
8556
86- path = line . Replace ( "BINARY_PATH_NAME : " , "" ) ;
57+ public static bool GetServiceStoragePath ( string serviceName , out string ? storage )
58+ {
59+ if ( GetServiceCommandLine ( serviceName , out var binpath ) &&
60+ binpath . Contains ( "--storage=\" " ) )
61+ {
62+ var start = binpath . IndexOf ( "--storage=\" " , StringComparison . Ordinal ) + 11 ;
63+ var chop = binpath . Substring ( start ) ;
64+ storage = chop . Substring ( 0 , chop . IndexOf ( '"' ) ) ;
8765 return true ;
8866 }
8967
90- public static bool GetServiceStoragePath ( string serviceName , out string ? storage )
91- {
92- if ( serviceName == null ) throw new ArgumentNullException ( nameof ( serviceName ) ) ;
93-
94- if ( GetServiceCommandLine ( serviceName , out var binpath ) &&
95- binpath . Contains ( "--storage=\" " ) )
96- {
97- var start = binpath . IndexOf ( "--storage=\" " , StringComparison . Ordinal ) + 11 ;
98- var chop = binpath . Substring ( start ) ;
99- storage = chop . Substring ( 0 , chop . IndexOf ( '"' ) ) ;
100- return true ;
101- }
102-
103- storage = null ;
104- return false ;
105- }
68+ storage = null ;
69+ return false ;
10670 }
107- }
108-
71+ }
0 commit comments