skst
skst

Reputation: 627

How to get the path to the file the user uses to launch the Windows application associated with that file type?

I have a WPF application written in C# 11 targeting .NET 7. It will be packaged as MSIX and published to the Microsoft Store. (Although, I am side-loading it for now.) I have added a File Type Association for .xyz files to the package manifest.

I sideload the application on my Windows 11 machine successfully. Then, I double-click a .xyz file. Windows opens my program, as expected.

However, I cannot retrieve the path to the file that caused the application to open.

I have tried the following:

  1. Override Application.OnStartup(StartupEventArgs e) and use e.Args.

Args contains only one argument, and it is of the form: -ServerName:App.AppXkxw1e314qzr8fyhwy5fza7r57vbx9dph.mca

  1. Call Environment.GetCommandLineArgs().

This returns two arguments. The first one is the path to the application's DLL (of the form C:\Program Files\WindowsApps\BLAHBLAHBLAH_1.0.1.0_x64__dijkdkmnhep\MYAPP\MYAPP.dll), and the second is the same as the only argument in StartupEventArgs.Args. (See #1.)

  1. Create Program.cs and override Program.Main(string[] args).

args is empty.

  1. Call Windows.ApplicationModel.AppInstance.GetActivatedEventArgs()

This returns null.

  1. Access AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0].

In .NET 7, AppDomainSetup no longer has the ActivationArguments property.

References

Handle Protocol Activation and Redirection in Packaged Apps

How do I identify the file that launched my application

Get the path of the file that launched my app

Get file that the user opened the application with

Get path+filename of file that was opened with my application

Thank you for any assistance or ideas you may have. I have run out of things to try and places to Google/Bing.

Upvotes: 1

Views: 372

Answers (1)

skst
skst

Reputation: 627

I guess talking to the duck helped because I was finally able to solve the issue after writing this up.

Here is how to add a Declaration of type File Type Association to the Package.appxmanifest file in Visual Studio 2022:

  • Display name: Some text describing the file type
  • Logo: optional, but if you add one and then delete it, it leaves an empty <uap:Logo /> element in Package.appxmanifest which you must delete manually.
  • Info tip: optional
  • Name: Extension without the leading period (e.g., txt)
  • Supported file type
    • Content type: Known MIME type (e.g., text/html)
    • File type: Extension with leading period (e.g., .txt)

Example

        <uap:Extension Category="windows.fileTypeAssociation">
          <uap:FileTypeAssociation Name="custom">
            <uap:SupportedFileTypes>
              <uap:FileType ContentType="text/xml">.custom</uap:FileType>
            </uap:SupportedFileTypes>
            <uap:DisplayName>My Custom File Type</uap:DisplayName>
            <uap:EditFlags OpenIsSafe="true"/>
          </uap:FileTypeAssociation>
        </uap:Extension>

Once that is all sorted, protected override void OnStartup(StartupEventArgs e) returns the path to the file the user opened as the first argument in e.Args.

In addition, Environment.GetCommandLineArgs() returns the path as the second argument. (The first argument is the path of the DLL.)

Error Messages

This error message refers to the empty <uap:Logo /> element. Note that the error is in Package.appxmanifest, not in the app.manifest file, nor is it on line 38. It would be nice if the package errors were specific and accurate.

Error info: error C00CE169: App manifest validation error: The app manifest must be valid as per schema: Line 38, Column 14, Reason: '' violates minLength constraint of '1'.

References

After pouring through dozens of less helpful ones, these sites helped me resolve the issue.

Upvotes: 1

Related Questions