7
7
using Microsoft . Windows . AppLifecycle ;
8
8
using System ;
9
9
using System . Diagnostics ;
10
+ using System . IO ;
10
11
using System . Linq ;
11
12
using System . Threading ;
12
13
using System . Threading . Tasks ;
@@ -26,64 +27,10 @@ private static void Main()
26
27
{
27
28
WinRT . ComWrappersSupport . InitializeComWrappers ( ) ;
28
29
29
- var proc = System . Diagnostics . Process . GetCurrentProcess ( ) ;
30
+ var proc = Process . GetCurrentProcess ( ) ;
30
31
var alwaysOpenNewInstance = ApplicationData . Current . LocalSettings . Values . Get ( "AlwaysOpenANewInstance" , false ) ;
31
32
var activatedArgs = AppInstance . GetCurrent ( ) . GetActivatedEventArgs ( ) ;
32
33
33
- if ( ! alwaysOpenNewInstance )
34
- {
35
- if ( activatedArgs . Kind is ExtendedActivationKind . Launch )
36
- {
37
- var launchArgs = activatedArgs . Data as ILaunchActivatedEventArgs ;
38
-
39
- if ( false )
40
- {
41
- // WINUI3: remove
42
- }
43
- else
44
- {
45
- if ( false )
46
- {
47
- // WINUI3: remove
48
- }
49
- else
50
- {
51
- var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
52
- var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
53
- if ( ! instance . IsCurrent && ! string . IsNullOrWhiteSpace ( launchArgs . Arguments ) )
54
- {
55
- RedirectActivationTo ( instance , activatedArgs ) ;
56
- return ;
57
- }
58
- }
59
- }
60
- }
61
- else if ( activatedArgs . Data is IProtocolActivatedEventArgs protocolArgs )
62
- {
63
- var parsedArgs = protocolArgs . Uri . Query . TrimStart ( '?' ) . Split ( '=' ) ;
64
- if ( parsedArgs . Length == 2 && parsedArgs [ 0 ] == "cmd" ) // Treat as command line launch
65
- {
66
- var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
67
- var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
68
- if ( ! instance . IsCurrent )
69
- {
70
- RedirectActivationTo ( instance , activatedArgs ) ;
71
- return ;
72
- }
73
- }
74
- }
75
- else if ( activatedArgs . Data is IFileActivatedEventArgs )
76
- {
77
- var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
78
- var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
79
- if ( ! instance . IsCurrent )
80
- {
81
- RedirectActivationTo ( instance , activatedArgs ) ;
82
- return ;
83
- }
84
- }
85
- }
86
-
87
34
if ( activatedArgs . Data is ICommandLineActivatedEventArgs cmdLineArgs )
88
35
{
89
36
var operation = cmdLineArgs . Operation ;
@@ -123,6 +70,58 @@ private static void Main()
123
70
}
124
71
}
125
72
73
+ if ( activatedArgs . Data is ILaunchActivatedEventArgs tileArgs )
74
+ {
75
+ if ( tileArgs . Arguments is not null &&
76
+ ! tileArgs . Arguments . Contains ( $ "files.exe", StringComparison . OrdinalIgnoreCase ) &&
77
+ new [ ] { ".exe" , ".bat" , ".cmd" } . Contains ( Path . GetExtension ( tileArgs . Arguments ) , StringComparer . OrdinalIgnoreCase ) )
78
+ {
79
+ if ( File . Exists ( tileArgs . Arguments ) )
80
+ {
81
+ OpenFileFromTile ( tileArgs . Arguments ) ;
82
+ return ;
83
+ }
84
+ }
85
+ }
86
+
87
+ if ( ! alwaysOpenNewInstance )
88
+ {
89
+ if ( activatedArgs . Data is ILaunchActivatedEventArgs launchArgs )
90
+ {
91
+ var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
92
+ var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
93
+ if ( ! instance . IsCurrent && ! string . IsNullOrWhiteSpace ( launchArgs . Arguments ) )
94
+ {
95
+ RedirectActivationTo ( instance , activatedArgs ) ;
96
+ return ;
97
+ }
98
+ }
99
+ else if ( activatedArgs . Data is IProtocolActivatedEventArgs protocolArgs )
100
+ {
101
+ var parsedArgs = protocolArgs . Uri . Query . TrimStart ( '?' ) . Split ( '=' ) ;
102
+ if ( parsedArgs . Length == 2 && parsedArgs [ 0 ] == "cmd" ) // Treat as command line launch
103
+ {
104
+ var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
105
+ var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
106
+ if ( ! instance . IsCurrent )
107
+ {
108
+ RedirectActivationTo ( instance , activatedArgs ) ;
109
+ return ;
110
+ }
111
+ }
112
+ }
113
+ else if ( activatedArgs . Data is IFileActivatedEventArgs )
114
+ {
115
+ var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
116
+ var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
117
+ if ( ! instance . IsCurrent )
118
+ {
119
+ RedirectActivationTo ( instance , activatedArgs ) ;
120
+ return ;
121
+ }
122
+ }
123
+ }
124
+
126
125
var currentInstance = AppInstance . FindOrRegisterForKey ( ( - proc . Id ) . ToString ( ) ) ;
127
126
if ( currentInstance . IsCurrent )
128
127
{
@@ -148,34 +147,39 @@ private static void OnActivated(object? sender, AppActivationArguments args)
148
147
}
149
148
}
150
149
151
- public static void OpenShellCommandInExplorer ( string shellCommand , int pid )
152
- {
153
- Win32API . OpenFolderInExistingShellWindow ( shellCommand ) ;
154
- SafetyExtensions . IgnoreExceptions ( ( ) =>
155
- {
156
- using var process = Process . GetProcessById ( pid ) ;
157
- process ? . Kill ( ) ;
158
- } ) ;
159
- }
160
-
161
- private static IntPtr redirectEventHandle = IntPtr . Zero ;
150
+ private const uint CWMO_DEFAULT = 0 ;
151
+ private const uint INFINITE = 0xFFFFFFFF ;
162
152
163
153
// Do the redirection on another thread, and use a non-blocking
164
154
// wait method to wait for the redirection to complete.
165
155
public static void RedirectActivationTo (
166
156
AppInstance keyInstance , AppActivationArguments args )
167
157
{
168
- redirectEventHandle = CreateEvent ( IntPtr . Zero , true , false , null ) ;
158
+ IntPtr eventHandle = CreateEvent ( IntPtr . Zero , true , false , null ) ;
169
159
Task . Run ( ( ) =>
170
160
{
171
161
keyInstance . RedirectActivationToAsync ( args ) . AsTask ( ) . Wait ( ) ;
172
- SetEvent ( redirectEventHandle ) ;
162
+ SetEvent ( eventHandle ) ;
163
+ } ) ;
164
+ _ = CoWaitForMultipleObjects (
165
+ CWMO_DEFAULT , INFINITE , 1 ,
166
+ new IntPtr [ ] { eventHandle } , out uint handleIndex ) ;
167
+ }
168
+
169
+ public static void OpenShellCommandInExplorer ( string shellCommand , int pid )
170
+ => Win32API . OpenFolderInExistingShellWindow ( shellCommand ) ;
171
+
172
+ public static void OpenFileFromTile ( string filePath )
173
+ {
174
+ IntPtr eventHandle = CreateEvent ( IntPtr . Zero , true , false , null ) ;
175
+ Task . Run ( ( ) =>
176
+ {
177
+ LaunchHelper . LaunchAppAsync ( filePath , null , null ) . Wait ( ) ;
178
+ SetEvent ( eventHandle ) ;
173
179
} ) ;
174
- uint CWMO_DEFAULT = 0 ;
175
- uint INFINITE = 0xFFFFFFFF ;
176
180
_ = CoWaitForMultipleObjects (
177
181
CWMO_DEFAULT , INFINITE , 1 ,
178
- new IntPtr [ ] { redirectEventHandle } , out uint handleIndex ) ;
182
+ new IntPtr [ ] { eventHandle } , out uint handleIndex ) ;
179
183
}
180
184
}
181
185
}
0 commit comments