@@ -101,18 +101,11 @@ static bool ProcessPathPredicate(Process p)
101
101
102
102
var OpenTabInExistingInstance = ApplicationData . Current . LocalSettings . Values . Get ( "OpenTabInExistingInstance" , true ) ;
103
103
var activatedArgs = AppInstance . GetCurrent ( ) . GetActivatedEventArgs ( ) ;
104
+ var commandLineArgs = GetCommandLineArgs ( activatedArgs ) ;
104
105
105
- // WINUI3: When launching from commandline the argument is not ICommandLineActivatedEventArgs (#10370)
106
- var isCommadLineLaunch = activatedArgs . Data is ILaunchActivatedEventArgs args &&
107
- args . Arguments is not null &&
108
- ( CommandLineParser . SplitArguments ( args . Arguments , true ) [ 0 ] . EndsWith ( $ "files.exe", StringComparison . OrdinalIgnoreCase )
109
- || CommandLineParser . SplitArguments ( args . Arguments , true ) [ 0 ] . EndsWith ( $ "files", StringComparison . OrdinalIgnoreCase ) ) ;
110
-
111
- if ( activatedArgs . Data is ICommandLineActivatedEventArgs || isCommadLineLaunch )
106
+ if ( commandLineArgs is not null )
112
107
{
113
- var cmdLineArgs = activatedArgs . Data as ICommandLineActivatedEventArgs ;
114
- var cmdLineLaunchArgs = activatedArgs . Data as ILaunchActivatedEventArgs ;
115
- var parsedCommands = CommandLineParser . ParseUntrustedCommands ( cmdLineArgs ? . Operation . Arguments ?? cmdLineLaunchArgs ! . Arguments ) ;
108
+ var parsedCommands = CommandLineParser . ParseUntrustedCommands ( commandLineArgs ) ;
116
109
117
110
if ( parsedCommands is not null )
118
111
{
@@ -124,11 +117,8 @@ args.Arguments is not null &&
124
117
if ( ! Constants . UserEnvironmentPaths . ShellPlaces . ContainsKey ( command . Payload . ToUpperInvariant ( ) ) )
125
118
{
126
119
OpenShellCommandInExplorer ( command . Payload , Environment . ProcessId ) ;
127
-
128
- // Exit
129
120
return ;
130
121
}
131
-
132
122
break ;
133
123
134
124
default :
@@ -154,7 +144,6 @@ args.Arguments is not null &&
154
144
else if ( activatedArgs . Data is ILaunchActivatedEventArgs tileArgs )
155
145
{
156
146
if ( tileArgs . Arguments is not null &&
157
- ! tileArgs . Arguments . Contains ( $ "files.exe", StringComparison . OrdinalIgnoreCase ) &&
158
147
FileExtensionHelpers . IsExecutableFile ( tileArgs . Arguments ) )
159
148
{
160
149
if ( File . Exists ( tileArgs . Arguments ) )
@@ -165,7 +154,7 @@ args.Arguments is not null &&
165
154
}
166
155
}
167
156
168
- if ( OpenTabInExistingInstance && ! isCommadLineLaunch )
157
+ if ( OpenTabInExistingInstance && commandLineArgs is null )
169
158
{
170
159
if ( activatedArgs . Data is ILaunchActivatedEventArgs launchArgs )
171
160
{
@@ -180,8 +169,7 @@ args.Arguments is not null &&
180
169
else if ( activatedArgs . Data is IProtocolActivatedEventArgs protocolArgs )
181
170
{
182
171
var parsedArgs = protocolArgs . Uri . Query . TrimStart ( '?' ) . Split ( '=' ) ;
183
- if ( ( parsedArgs . Length == 2 && parsedArgs [ 0 ] == "cmd" ) ||
184
- parsedArgs . Length == 1 ) // Treat Win+E & Open file location as command line launch
172
+ if ( parsedArgs . Length == 1 )
185
173
{
186
174
var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
187
175
var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
@@ -219,6 +207,26 @@ args.Arguments is not null &&
219
207
} ) ;
220
208
}
221
209
210
+ /// <summary>
211
+ /// Gets command line args from AppActivationArguments
212
+ /// Command line args can be ILaunchActivatedEventArgs, ICommandLineActivatedEventArgs or IProtocolActivatedEventArgs
213
+ /// </summary>
214
+ private static string ? GetCommandLineArgs ( AppActivationArguments activatedArgs )
215
+ {
216
+ // WINUI3: When launching from commandline the argument is not ICommandLineActivatedEventArgs (#10370)
217
+ var cmdLaunchArgs = activatedArgs . Data is ILaunchActivatedEventArgs launchArgs &&
218
+ launchArgs . Arguments is not null &&
219
+ CommandLineParser . SplitArguments ( launchArgs . Arguments , true ) . FirstOrDefault ( ) is string arg0 &&
220
+ ( arg0 . EndsWith ( $ "files.exe", StringComparison . OrdinalIgnoreCase ) ||
221
+ arg0 . EndsWith ( $ "files", StringComparison . OrdinalIgnoreCase ) ) ? launchArgs . Arguments : null ;
222
+ var cmdProtocolArgs = activatedArgs . Data is IProtocolActivatedEventArgs protocolArgs &&
223
+ protocolArgs . Uri . Query . TrimStart ( '?' ) . Split ( '=' ) is string [ ] parsedArgs &&
224
+ parsedArgs . Length == 2 && parsedArgs [ 0 ] == "cmd" ? Uri . UnescapeDataString ( parsedArgs [ 1 ] ) : null ;
225
+ var cmdLineArgs = activatedArgs . Data is ICommandLineActivatedEventArgs cmdArgs ? cmdArgs . Operation . Arguments : null ;
226
+
227
+ return cmdLaunchArgs ?? cmdProtocolArgs ?? cmdLineArgs ;
228
+ }
229
+
222
230
/// <summary>
223
231
/// Gets invoked when the application is activated.
224
232
/// </summary>
0 commit comments