Skip to content

Commit 7222bfb

Browse files
authored
Parse command line before App launch (#746)
1 parent 35f6f00 commit 7222bfb

File tree

3 files changed

+61
-45
lines changed

3 files changed

+61
-45
lines changed

Files/App.xaml.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,6 @@ public static IShellPage CurrentInstance
6464

6565
public App()
6666
{
67-
var args = Environment.GetCommandLineArgs();
68-
69-
if (args.Length == 2)
70-
{
71-
var parsedCommands = CommandLineParser.ParseUntrustedCommands(args);
72-
73-
if (parsedCommands != null && parsedCommands.Count > 0)
74-
{
75-
foreach (var command in parsedCommands)
76-
{
77-
switch (command.Type)
78-
{
79-
case ParsedCommandType.ExplorerShellCommand:
80-
var proc = Process.GetCurrentProcess();
81-
OpenShellCommandInExplorer(command.Payload, proc.Id).GetAwaiter().GetResult();
82-
83-
//this is useless.
84-
Exit();
85-
return;
86-
default:
87-
break;
88-
}
89-
}
90-
}
91-
92-
93-
}
94-
9567
InitializeComponent();
9668
Suspending += OnSuspending;
9769

@@ -340,15 +312,6 @@ protected override async void OnActivated(IActivatedEventArgs args)
340312
Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;
341313
}
342314

343-
public static async Task OpenShellCommandInExplorer(string shellCommand, int pid)
344-
{
345-
System.Diagnostics.Debug.WriteLine("Launching shell command in FullTrustProcess");
346-
ApplicationData.Current.LocalSettings.Values["ShellCommand"] = shellCommand;
347-
ApplicationData.Current.LocalSettings.Values["Arguments"] = "ShellCommand";
348-
ApplicationData.Current.LocalSettings.Values["pid"] = pid;
349-
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
350-
}
351-
352315
private void TryEnablePrelaunch()
353316
{
354317
Windows.ApplicationModel.Core.CoreApplication.EnablePrelaunch(true);

Files/Files.csproj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
3131
<DebugSymbols>true</DebugSymbols>
3232
<OutputPath>bin\x86\Debug\</OutputPath>
33-
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
33+
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
3434
<NoWarn>;2008</NoWarn>
3535
<DebugType>full</DebugType>
3636
<PlatformTarget>x86</PlatformTarget>
@@ -41,7 +41,7 @@
4141
</PropertyGroup>
4242
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
4343
<OutputPath>bin\x86\Release\</OutputPath>
44-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
44+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
4545
<Optimize>true</Optimize>
4646
<NoWarn>;2008</NoWarn>
4747
<DebugType>pdbonly</DebugType>
@@ -55,7 +55,7 @@
5555
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
5656
<DebugSymbols>true</DebugSymbols>
5757
<OutputPath>bin\ARM\Debug\</OutputPath>
58-
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
58+
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
5959
<NoWarn>;2008</NoWarn>
6060
<DebugType>full</DebugType>
6161
<PlatformTarget>ARM</PlatformTarget>
@@ -66,7 +66,7 @@
6666
</PropertyGroup>
6767
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
6868
<OutputPath>bin\ARM\Release\</OutputPath>
69-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
69+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
7070
<Optimize>true</Optimize>
7171
<NoWarn>;2008</NoWarn>
7272
<DebugType>pdbonly</DebugType>
@@ -80,7 +80,7 @@
8080
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM64'">
8181
<DebugSymbols>true</DebugSymbols>
8282
<OutputPath>bin\ARM64\Debug\</OutputPath>
83-
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
83+
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
8484
<NoWarn>;2008</NoWarn>
8585
<DebugType>full</DebugType>
8686
<PlatformTarget>ARM64</PlatformTarget>
@@ -92,7 +92,7 @@
9292
</PropertyGroup>
9393
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM64'">
9494
<OutputPath>bin\ARM64\Release\</OutputPath>
95-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
95+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
9696
<Optimize>true</Optimize>
9797
<NoWarn>;2008</NoWarn>
9898
<DebugType>pdbonly</DebugType>
@@ -106,7 +106,7 @@
106106
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
107107
<DebugSymbols>true</DebugSymbols>
108108
<OutputPath>bin\x64\Debug\</OutputPath>
109-
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
109+
<DefineConstants>TRACE;DEBUG;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
110110
<NoWarn>;2008</NoWarn>
111111
<DebugType>full</DebugType>
112112
<PlatformTarget>x64</PlatformTarget>
@@ -117,7 +117,7 @@
117117
</PropertyGroup>
118118
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
119119
<OutputPath>bin\x64\Release\</OutputPath>
120-
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
120+
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP;DISABLE_XAML_GENERATED_MAIN</DefineConstants>
121121
<Optimize>true</Optimize>
122122
<NoWarn>;2008</NoWarn>
123123
<DebugType>pdbonly</DebugType>
@@ -156,6 +156,7 @@
156156
<Compile Include="Helpers\StringExtensions.cs" />
157157
<Compile Include="Helpers\NegateConverter.cs" />
158158
<Compile Include="Helpers\ThemeHelper.cs" />
159+
<Compile Include="Program.cs" />
159160
<Compile Include="ResourceController.cs" />
160161
<Compile Include="UserControls\NavigationToolbar\INavigationToolbar.cs" />
161162
<Compile Include="UserControls\NavigationToolbar\ModernNavigationToolbar.xaml.cs">

Files/Program.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Files.CommandLine;
2+
using System;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Windows.ApplicationModel;
6+
using Windows.Storage;
7+
using Windows.UI.Xaml;
8+
9+
namespace Files
10+
{
11+
class Program
12+
{
13+
static void Main()
14+
{
15+
var args = Environment.GetCommandLineArgs();
16+
17+
if (args.Length == 2)
18+
{
19+
var parsedCommands = CommandLineParser.ParseUntrustedCommands(args);
20+
21+
if (parsedCommands != null && parsedCommands.Count > 0)
22+
{
23+
foreach (var command in parsedCommands)
24+
{
25+
switch (command.Type)
26+
{
27+
case ParsedCommandType.ExplorerShellCommand:
28+
var proc = System.Diagnostics.Process.GetCurrentProcess();
29+
OpenShellCommandInExplorer(command.Payload, proc.Id).GetAwaiter().GetResult();
30+
//Exit..
31+
32+
return;
33+
default:
34+
break;
35+
}
36+
}
37+
}
38+
}
39+
40+
Application.Start(_ => new App());
41+
}
42+
43+
public static async Task OpenShellCommandInExplorer(string shellCommand, int pid)
44+
{
45+
System.Diagnostics.Debug.WriteLine("Launching shell command in FullTrustProcess");
46+
ApplicationData.Current.LocalSettings.Values["ShellCommand"] = shellCommand;
47+
ApplicationData.Current.LocalSettings.Values["Arguments"] = "ShellCommand";
48+
ApplicationData.Current.LocalSettings.Values["pid"] = pid;
49+
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)