-2

I am able to set my .mdb files to "open with" my winforms application, but that only opens the winforms application without loading the file. Is there any event that is triggered upon opening the application through a file that will allow me to grab the filename/file-directory to load it into my app?

Update:

apparently this can be done for "Windows Store Apps": File "Open with" my Windows Store App

But it seems as though my winforms project lacks a package.appxmanifest to edit these settings from, nor does my project have access to the Windows.ApplicationModel.Activation.FileActivatedEventArgs and Windows.UI.Xaml.Application.OnFileActivated APIs needed for this method.

DED
  • 391
  • 2
  • 12

1 Answers1

1

When you double-click an mdb file, your application will be executed with mdb file argument. Similar to calling your app from command line:

> your-app.exe "test.mdb"

You should use the args in the Main method.

class Program
{
    static void Main(string[] args)
    {
        // check that user provided a file to open
        if (args != null && args.Length > 0)
        {
            var mdbFilePath = args[0];
            // TODO: read the file
        }

        // ...
Murat
  • 36
  • 5
  • This didn't work for whatever reason (`args` was an empty array) but it led me to the alternative method of fetching these args `Environment.GetCommandLineArgs()[0]` which did work. – DED Feb 06 '22 at 07:08
  • turns out this only returns the path to the application directory and not the selected file the application was opened from. – DED Feb 06 '22 at 08:39
  • `Environment.GetCommandLineArgs()[0]` always returns the app path as the first element. You get the file that was passed as command line if you have setup your App's `ProgId` correctly in the Registry or your App is the `(Default)` associate in the `Shell->Open->Command` of the file's `ProgId`. In the form of `"The Full path of your app" %1` – Jimi Feb 06 '22 at 11:51
  • Of course, any command line argument is stored in `args[0]`, `args[1]` etc. and `.GetCommandLineArgs()[1]`, `.GetCommandLineArgs()[2]` etc. – Jimi Feb 06 '22 at 13:06
  • @Murat NVM this is the correct solution, `args` returns an empty array when it's run via the executable directly which led me to believe that it wasn't working at all when I was testing it in Visual Studio (since I assumed it would give me the directory regardless of where the application was opened from). I later found out with further testing that it does in fact contain the filename in `args[0]` when I open a file with the application. – DED Feb 07 '22 at 01:37