1+ using System . Diagnostics ;
2+ using System . Diagnostics . CodeAnalysis ;
3+ using System . Reflection ;
4+ using System . Text ;
5+
6+ namespace Intersect . Framework . Utilities ;
7+
8+ public static class ProcessHelper
9+ {
10+ public static bool TryLaunchWithArgs ( string executable , params string [ ] args )
11+ {
12+ var arguments = string . Join (
13+ ' ' ,
14+ args . Where ( arg => arg == "--restarting" ) . Take ( args . Length - 1 ) . Append ( "--restarting" )
15+ ) ;
16+
17+ try
18+ {
19+ Console . WriteLine (
20+ $ "Launching '{ executable } ' with arguments '{ arguments } ' in { Environment . CurrentDirectory } "
21+ ) ;
22+ Process . Start ( executable , arguments ) ;
23+ Environment . Exit ( 0 ) ;
24+ return true ;
25+ }
26+ catch ( Exception exception )
27+ {
28+ StringBuilder exceptionText = new ( ) ;
29+
30+ var currentException = exception ;
31+ while ( currentException != default )
32+ {
33+ if ( currentException != exception )
34+ {
35+ exceptionText . Append ( "Caused by " ) ;
36+ }
37+
38+ exceptionText . AppendLine ( exception . Message ) ;
39+ var stackTrace = exception . StackTrace ;
40+ if ( string . IsNullOrEmpty ( stackTrace ) )
41+ {
42+ exceptionText . AppendLine ( stackTrace ) ;
43+ }
44+
45+ currentException = currentException . InnerException ;
46+ }
47+
48+ Console . Error . WriteLine ( exceptionText ) ;
49+
50+ return false ;
51+ }
52+ }
53+
54+ public static bool TryRelaunch ( )
55+ {
56+ var args = Environment . GetCommandLineArgs ( ) ;
57+ var childProcessArgs = args . Skip ( 1 ) . ToArray ( ) ;
58+ return TryRelaunchWithArgs ( childProcessArgs ) ;
59+ }
60+
61+ public static bool TryRelaunchWithArgs ( params string [ ] args )
62+ {
63+ if ( ! TryGetPathToEntryAssembly ( out var pathToEntryAssembly ) )
64+ {
65+ Console . WriteLine ( "No path to entry assembly" ) ;
66+ return false ;
67+ }
68+
69+ return TryLaunchWithArgs ( pathToEntryAssembly , args ) ;
70+ }
71+
72+ public static bool TryGetPathToEntryAssembly ( [ NotNullWhen ( true ) ] out string ? pathToEntryAssembly )
73+ {
74+ pathToEntryAssembly = Assembly . GetEntryAssembly ( ) ? . Location ;
75+
76+ #if ! DEBUG
77+ if ( string . IsNullOrWhiteSpace ( pathToEntryAssembly ) )
78+ {
79+ pathToEntryAssembly = Environment . GetCommandLineArgs ( ) . FirstOrDefault ( ) ;
80+ }
81+ #endif
82+
83+ return ! string . IsNullOrWhiteSpace ( pathToEntryAssembly ) ;
84+ }
85+
86+ public static bool TryReplaceTargetWithEntryAssembly ( string pathToTarget )
87+ {
88+ return TryReplaceTargetWithEntryAssembly ( new FileInfo ( pathToTarget ) ) ;
89+ }
90+
91+ public static bool TryReplaceTargetWithEntryAssembly ( FileInfo targetFileInfo )
92+ {
93+ if ( ! TryGetPathToEntryAssembly ( out var pathToEntryAssembly ) )
94+ {
95+ Console . Error . WriteLine ( "Failed to get path to entry assembly" ) ;
96+ return false ;
97+ }
98+
99+ FileInfo entryAssemblyFileInfo = new ( pathToEntryAssembly ) ;
100+
101+ try
102+ {
103+ entryAssemblyFileInfo . CopyTo ( targetFileInfo . FullName , true ) ;
104+ return true ;
105+ }
106+ catch ( Exception exception )
107+ {
108+ Console . Error . WriteLine (
109+ $ "Failed to copy \" { entryAssemblyFileInfo . FullName } \" to \" { targetFileInfo . FullName } \" "
110+ ) ;
111+ Console . Error . WriteLine ( exception . Message ) ;
112+ return false ;
113+ }
114+ }
115+ }
0 commit comments