App.xaml.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Linq;
 7  
 8  using ManagedCommon;
 9  using Microsoft.UI.Dispatching;
10  using Microsoft.UI.Xaml;
11  
12  namespace FileLocksmithUI
13  {
14      /// <summary>
15      /// Provides application-specific behavior to supplement the default Application class.
16      /// </summary>
17      public partial class App : Application
18      {
19          /// <summary>
20          /// Initializes a new instance of the <see cref="App"/> class.
21          /// Initializes the singleton application object.  This is the first line of authored code
22          /// executed, and as such is the logical equivalent of main() or WinMain().
23          /// </summary>
24          public App()
25          {
26              string appLanguage = LanguageHelper.LoadLanguage();
27              if (!string.IsNullOrEmpty(appLanguage))
28              {
29                  Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage;
30              }
31  
32              Logger.InitializeLogger("\\File Locksmith\\FileLocksmithUI\\Logs");
33  
34              this.InitializeComponent();
35  
36              UnhandledException += App_UnhandledException;
37          }
38  
39          /// <summary>
40          /// Invoked when the application is launched normally by the end user.  Other entry points
41          /// will be used such as when the application is launched to open a specific file.
42          /// </summary>
43          /// <param name="args">Details about the launch request and process.</param>
44          protected override void OnLaunched(LaunchActivatedEventArgs args)
45          {
46              if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredFileLocksmithEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
47              {
48                  Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");
49                  Environment.Exit(0); // Current.Exit won't work until there's a window opened.
50                  return;
51              }
52  
53              bool isElevated = PowerToys.FileLocksmithLib.Interop.NativeMethods.IsProcessElevated();
54  
55              if (isElevated)
56              {
57                  if (!PowerToys.FileLocksmithLib.Interop.NativeMethods.SetDebugPrivilege())
58                  {
59                      Logger.LogWarning("Couldn't set debug privileges to see system processes.");
60                  }
61              }
62  
63              _window = new MainWindow(isElevated);
64              _window.Activate();
65          }
66  
67          private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
68          {
69              Logger.LogError("Unhandled exception", e.Exception);
70          }
71  
72          private Window _window;
73      }
74  }