From 02ff6f83d4afbe4d8a02dcfa886606519028be18 Mon Sep 17 00:00:00 2001 From: Saravanan Ganapathi Date: Thu, 26 Feb 2026 18:16:28 +0530 Subject: [PATCH 1/2] Code Quality: Improve startup error message when Windows App Runtime fails to load --- .../Helpers/Win32/Win32PInvoke.Consts.cs | 2 ++ .../Helpers/Win32/Win32PInvoke.Methods.cs | 3 +++ src/Files.App/Program.cs | 24 ++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs b/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs index d36433d37833..291df712ac45 100644 --- a/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs +++ b/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs @@ -64,5 +64,7 @@ public static partial class Win32PInvoke public const string LOCALE_NAME_USER_DEFAULT = null; public const string LOCALE_NAME_INVARIANT = ""; public const string LOCALE_NAME_SYSTEM_DEFAULT = "!sys-default-locale"; + + public const uint MB_ICONERROR = 0x10; } } diff --git a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs index 0c214c2d2c49..e1ef2ff4c771 100644 --- a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs +++ b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs @@ -341,5 +341,8 @@ out IntPtr pszPath // cryptui.dll [DllImport("cryptui.dll", SetLastError = true, CharSet = CharSet.Auto)] public unsafe static extern bool CryptUIDlgViewSignerInfo(CRYPTUI_VIEWSIGNERINFO_STRUCT* pViewInfo); + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + public static extern int MessageBoxW(IntPtr hWnd, string text, string caption, uint type); } } diff --git a/src/Files.App/Program.cs b/src/Files.App/Program.cs index 5676b750ed43..2dfd9523e3d1 100644 --- a/src/Files.App/Program.cs +++ b/src/Files.App/Program.cs @@ -6,6 +6,7 @@ using Microsoft.UI.Xaml; using Microsoft.Windows.AppLifecycle; using System.IO; +using System.Runtime.InteropServices; using System.Text; using Windows.ApplicationModel.Activation; using Windows.Storage; @@ -20,6 +21,11 @@ namespace Files.App /// internal sealed class Program { + // These strings are intentionally hardcoded and cannot be moved to resource files. + // The Windows App Runtime (which powers the resource loading system) may itself be unavailable at this point + private const string MissingRuntimeMessage = "Files failed to start. A required Windows component could not be loaded. Try reinstalling Files from the Microsoft Store."; + private const string MissingRuntimeTitle = "Files - Startup Error"; + public static Semaphore? Pool { get; set; } static Program() @@ -101,7 +107,23 @@ static bool ProcessPathPredicate(Process p) //Server.AppInstanceMonitor.StartMonitor(Environment.ProcessId); var OpenTabInExistingInstance = ApplicationData.Current.LocalSettings.Values.Get("OpenTabInExistingInstance", true); - var activatedArgs = AppInstance.GetCurrent().GetActivatedEventArgs(); + + AppActivationArguments activatedArgs; + try + { + activatedArgs = AppInstance.GetCurrent().GetActivatedEventArgs(); + } + catch (COMException ex) when (ex.HResult == unchecked((int)0x80040154)) + { + _ = Win32PInvoke.MessageBoxW( + IntPtr.Zero, + MissingRuntimeMessage, + MissingRuntimeTitle, + Win32PInvoke.MB_ICONERROR); + + throw new InvalidOperationException(MissingRuntimeMessage, ex); + } + var commandLineArgs = GetCommandLineArgs(activatedArgs); if (commandLineArgs is not null) From 8c322ebe4fd548c4410d304b98395ec3425a9dc1 Mon Sep 17 00:00:00 2001 From: Saravanan Ganapathi Date: Fri, 27 Feb 2026 16:27:07 +0530 Subject: [PATCH 2/2] Code Quality: Improve startup error message when Windows App Runtime fails to load --- src/Files.App.CsWin32/NativeMethods.txt | 1 + src/Files.App/Constants.cs | 8 ++++++++ .../Helpers/Win32/Win32PInvoke.Consts.cs | 2 -- .../Helpers/Win32/Win32PInvoke.Methods.cs | 3 --- src/Files.App/Program.cs | 18 +++++++----------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index 9f10fdd3870b..9d900f53d0e9 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -267,3 +267,4 @@ WINTRUST_DATA HCERTSTORE CERT_QUERY_ENCODING_TYPE CertGetNameString +MessageBox diff --git a/src/Files.App/Constants.cs b/src/Files.App/Constants.cs index c3a531e502dd..578c8951644f 100644 --- a/src/Files.App/Constants.cs +++ b/src/Files.App/Constants.cs @@ -282,5 +282,13 @@ public static class Distributions "FilesDev", // dev }; } + + public static class Startup + { + // These strings are intentionally hardcoded and cannot be moved to resource files. + // The Windows App Runtime (which powers the resource loading system) may itself be unavailable at this point + public const string MissingRuntimeMessage = "Files failed to start. A required Windows component could not be loaded. Try reinstalling Files from the Microsoft Store or from https://files.community/download"; + public const string MissingRuntimeTitle = "Files - Startup Error"; + } } } diff --git a/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs b/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs index 291df712ac45..d36433d37833 100644 --- a/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs +++ b/src/Files.App/Helpers/Win32/Win32PInvoke.Consts.cs @@ -64,7 +64,5 @@ public static partial class Win32PInvoke public const string LOCALE_NAME_USER_DEFAULT = null; public const string LOCALE_NAME_INVARIANT = ""; public const string LOCALE_NAME_SYSTEM_DEFAULT = "!sys-default-locale"; - - public const uint MB_ICONERROR = 0x10; } } diff --git a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs index e1ef2ff4c771..0c214c2d2c49 100644 --- a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs +++ b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs @@ -341,8 +341,5 @@ out IntPtr pszPath // cryptui.dll [DllImport("cryptui.dll", SetLastError = true, CharSet = CharSet.Auto)] public unsafe static extern bool CryptUIDlgViewSignerInfo(CRYPTUI_VIEWSIGNERINFO_STRUCT* pViewInfo); - - [DllImport("user32.dll", CharSet = CharSet.Unicode)] - public static extern int MessageBoxW(IntPtr hWnd, string text, string caption, uint type); } } diff --git a/src/Files.App/Program.cs b/src/Files.App/Program.cs index 2dfd9523e3d1..91702da505af 100644 --- a/src/Files.App/Program.cs +++ b/src/Files.App/Program.cs @@ -8,6 +8,7 @@ using System.IO; using System.Runtime.InteropServices; using System.Text; +using Windows.Win32.UI.WindowsAndMessaging; using Windows.ApplicationModel.Activation; using Windows.Storage; @@ -21,11 +22,6 @@ namespace Files.App /// internal sealed class Program { - // These strings are intentionally hardcoded and cannot be moved to resource files. - // The Windows App Runtime (which powers the resource loading system) may itself be unavailable at this point - private const string MissingRuntimeMessage = "Files failed to start. A required Windows component could not be loaded. Try reinstalling Files from the Microsoft Store."; - private const string MissingRuntimeTitle = "Files - Startup Error"; - public static Semaphore? Pool { get; set; } static Program() @@ -115,13 +111,13 @@ static bool ProcessPathPredicate(Process p) } catch (COMException ex) when (ex.HResult == unchecked((int)0x80040154)) { - _ = Win32PInvoke.MessageBoxW( - IntPtr.Zero, - MissingRuntimeMessage, - MissingRuntimeTitle, - Win32PInvoke.MB_ICONERROR); + Windows.Win32.PInvoke.MessageBox( + default, + Constants.Startup.MissingRuntimeMessage, + Constants.Startup.MissingRuntimeTitle, + MESSAGEBOX_STYLE.MB_ICONERROR); - throw new InvalidOperationException(MissingRuntimeMessage, ex); + throw new InvalidOperationException(Constants.Startup.MissingRuntimeMessage, ex); } var commandLineArgs = GetCommandLineArgs(activatedArgs);