Search code examples
c#wpfcommand-line-argumentsopen-with

C# WPF App silently crashes if I use "open with"


I am making a text editor in C# WPF. After publishing the project to a folder, I can double-click the .exe, it runs fine, and I can load files using my Open (CTRL+O) function perfectly fine.

I want to use Open with > Choose another app > More apps > Look for another app on this PC (I'm not worrying about file associations yet - during development this is fine)

Here's the problem - no matter what I do to my code, I can't get my app to do anything useful when I use this method to open it. What happens is it brings up a randomly sized white window and looks like it's busy - after 2 seconds it closes itself. The name of the window is "%PathOfTheFileIOpened% - KuroNote", which seems to be automatic.

App.xaml

<Application x:Class="KuroNote.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:KuroNote"
             Startup="Application_Startup"
             ShutdownMode="OnMainWindowClose">
    <Application.Resources>
         
    </Application.Resources>
</Application>

App.xaml.cs

namespace KuroNote
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            MessageBox.Show("test");
            foreach (string s in e.Args)
            {
                MessageBox.Show(s);
            }
        }
    }
}

As you can see, I removed all the useful code to try and get it to do SOMETHING - I'm not even opening the main window, I just want it to output its command line arguments. When I use the "Look for another app on this PC" method, I don't even get the "test" message box, like you see is written above.

As you can see from this screenshot of the project properties, the startup object is also set to be the app.


I thought it might be permissions-related, but the executable and the file to be opened are never in protected administrator-only areas.

I've done tests before while debugging, retrieving command line arguments set in project properties > debug tab > "application arguments:", and those are returned just fine. Something seems to be different about opening using the "Look for another app on this PC" method, it's like it's doing more than just launching the app with a command line argument with the filepath.

Am I missing some kind of flag in App.xaml that enables "Open with" functionality?


It's a purely offline application, not a web app, and the files I get after publishing are just "KuroNote.deps.json", "KuroNote.dll", "KuroNote.exe", "KuroNote.pdb" and "KuroNote.runtimeconfig.json", no ".application" files that it sounds like "ClickOnce" uses. If I search for "ClickOnce" inside visual studio, it says I can install it, but it doesn't sound like it's already installed. Unless there's some hidden "use ClickOnce" setting that I need to disable?


Solution

  • It turns out that Windows 10 uses some kind of cache for "Open With..." functionality.

    So the reason why my above code looked like it was not executing, is because it actually wasn't executing - it was actually executing the same older version of the code (which contained some kind of error) every time.

    If you open a file (e.g. text file) with a specified .exe, that specified .exe file name (and presumably the assembly information (e.g. version number)) gets stored in a cache. The next time you open a file (e.g. text file) with that same specified .exe file name (and assembly information), the exact file you selected - that specified .exe file path will not actually execute. Instead, it will execute the same .exe that you used when you "Opened with" for the first time. Remember, every time we are actively browsing for a file, not selecting from the "Open With" list.

    One simple way to force Windows 10 to use the exact .exe you specify every time you use "Open With..." (and actively browse for a file), is to rename the .exe file to a name that has not been used before. I have not yet tested only changing the assembly information, so I'm not sure if that works.

    Once again this is one of those silent Windows 10 things that conflicts with common sense.