@@ -101,18 +101,11 @@ static bool ProcessPathPredicate(Process p)
101101
102102 var OpenTabInExistingInstance = ApplicationData . Current . LocalSettings . Values . Get ( "OpenTabInExistingInstance" , true ) ;
103103 var activatedArgs = AppInstance . GetCurrent ( ) . GetActivatedEventArgs ( ) ;
104+ var commandLineArgs = GetCommandLineArgs ( activatedArgs ) ;
104105
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 )
112107 {
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 ) ;
116109
117110 if ( parsedCommands is not null )
118111 {
@@ -124,11 +117,8 @@ args.Arguments is not null &&
124117 if ( ! Constants . UserEnvironmentPaths . ShellPlaces . ContainsKey ( command . Payload . ToUpperInvariant ( ) ) )
125118 {
126119 OpenShellCommandInExplorer ( command . Payload , Environment . ProcessId ) ;
127-
128- // Exit
129120 return ;
130121 }
131-
132122 break ;
133123
134124 default :
@@ -154,7 +144,6 @@ args.Arguments is not null &&
154144 else if ( activatedArgs . Data is ILaunchActivatedEventArgs tileArgs )
155145 {
156146 if ( tileArgs . Arguments is not null &&
157- ! tileArgs . Arguments . Contains ( $ "files.exe", StringComparison . OrdinalIgnoreCase ) &&
158147 FileExtensionHelpers . IsExecutableFile ( tileArgs . Arguments ) )
159148 {
160149 if ( File . Exists ( tileArgs . Arguments ) )
@@ -165,7 +154,7 @@ args.Arguments is not null &&
165154 }
166155 }
167156
168- if ( OpenTabInExistingInstance && ! isCommadLineLaunch )
157+ if ( OpenTabInExistingInstance && commandLineArgs is null )
169158 {
170159 if ( activatedArgs . Data is ILaunchActivatedEventArgs launchArgs )
171160 {
@@ -180,8 +169,7 @@ args.Arguments is not null &&
180169 else if ( activatedArgs . Data is IProtocolActivatedEventArgs protocolArgs )
181170 {
182171 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 )
185173 {
186174 var activePid = ApplicationData . Current . LocalSettings . Values . Get ( "INSTANCE_ACTIVE" , - 1 ) ;
187175 var instance = AppInstance . FindOrRegisterForKey ( activePid . ToString ( ) ) ;
@@ -219,6 +207,26 @@ args.Arguments is not null &&
219207 } ) ;
220208 }
221209
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+
222230 /// <summary>
223231 /// Gets invoked when the application is activated.
224232 /// </summary>
0 commit comments