|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Configuration;
|
| 4 | +using System.Globalization; |
| 5 | +using System.IO; |
4 | 6 | using System.Linq;
|
| 7 | +using System.Reflection; |
5 | 8 | using System.Threading.Tasks;
|
6 | 9 | using System.Windows;
|
| 10 | +using System.Windows.Media.Imaging; |
7 | 11 | using WinDirStat.Net.Wpf.Windows;
|
8 | 12 |
|
9 | 13 | namespace WinDirStat.Net.Wpf {
|
10 | 14 | /// <summary>
|
11 |
| - /// Interaction logic for App.xaml |
| 15 | + /// Interaction logic for App.xaml |
12 | 16 | /// </summary>
|
13 | 17 | public partial class App : Application {
|
| 18 | + #region Constants |
14 | 19 |
|
| 20 | + private static readonly string[] assemblyExtensions = { ".dll", ".exe" }; |
| 21 | + |
| 22 | + #endregion |
| 23 | + |
| 24 | + #region Constructors |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Constructs the app and sets up embedded assembly resolving. |
| 28 | + /// </summary> |
15 | 29 | public App() {
|
| 30 | + AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssemblies; |
| 31 | + // Call this to avoid referencing assemblies before the assembly resolver can be added. |
| 32 | + Initialize(); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Called to avoid referencing assemblies before the assembly resolver can be added. |
| 37 | + /// </summary> |
| 38 | + private void Initialize() { |
| 39 | + ErrorMessageBox.ProgramName = "WinDirStat.Net"; |
| 40 | + ErrorMessageBox.HyperlinkName = "GitHub Page"; |
| 41 | + ErrorMessageBox.HyperlinkUri = new Uri(@"https://github.com/trigger-death/WinDirStat.Net"); |
| 42 | + ErrorMessageBox.ErrorIcon = new BitmapImage(new Uri("pack://application:,,,/Resources/App.ico")); |
16 | 43 | ErrorMessageBox.GlobalHook(this);
|
17 | 44 | }
|
| 45 | + |
| 46 | + #endregion |
| 47 | + |
| 48 | + #region Event Handlers |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Resolves assemblies that may be embedded in the executable. |
| 52 | + /// </summary> |
| 53 | + private Assembly OnResolveAssemblies(object sender, ResolveEventArgs args) { |
| 54 | + AssemblyName assemblyName = new AssemblyName(args.Name); |
| 55 | + string assemblyPath; |
| 56 | + |
| 57 | + if (TryResolveAssembly(assemblyName, out Assembly assembly)) |
| 58 | + return assembly; |
| 59 | + assemblyPath = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; |
| 60 | + if (TryResolveAssembly(assemblyPath, assemblyName, out assembly)) |
| 61 | + return assembly; |
| 62 | + assemblyPath = CultureInfo.CurrentCulture.ToString(); |
| 63 | + if (TryResolveAssembly(assemblyPath, assemblyName, out assembly)) |
| 64 | + return assembly; |
| 65 | + |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + #endregion |
| 70 | + |
| 71 | + #region TryResolveAssembly |
| 72 | + |
| 73 | + private bool TryResolveAssembly(AssemblyName assemblyName, out Assembly assembly) { |
| 74 | + return TryResolveAssembly(null, assemblyName, out assembly); |
| 75 | + } |
| 76 | + private bool TryResolveAssembly(string path, AssemblyName assemblyName, out Assembly assembly) { |
| 77 | + foreach (string ext in assemblyExtensions) { |
| 78 | + string startPath = Path.Combine(AppContext.BaseDirectory, "bin"); |
| 79 | + if (path != null && !Path.IsPathRooted(path)) |
| 80 | + startPath = Path.Combine(startPath, path); |
| 81 | + |
| 82 | + path = Path.Combine(startPath, assemblyName.Name + ext); |
| 83 | + if (File.Exists(path)) { |
| 84 | + assembly = Assembly.LoadFile(path); |
| 85 | + return true; |
| 86 | + } |
| 87 | + } |
| 88 | + assembly = null; |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + #endregion |
18 | 93 | }
|
19 | 94 | }
|
0 commit comments