Search code examples
c#wpffile-association

How can I open a file via click it in windows with a WPF app?


I made a WPF program, just like the notepad in windows.

Now I modified the file associations of extension .txt to my program.

After I clicked a txt file, the problem comes: how can my WPF program get the path of the file? (I need to get the path to open the file.)

I consider it is an easy job. However, it seems there is not any tutorial about this in Google(maybe I miss its keyword).

Would you please help me? Thank you.


Solution

  • Thanks for @Fildor 's tutorial in https://sa.ndeep.me/post/how-to-create-smart-wpf-command-line-arguments/

    Others tutorial works also while this one is easier:

     public MainWindow()
            {
                string[] args = Environment.GetCommandLineArgs();
                foreach (string i in args)
                {
                    MessageBox.Show(i);
                }
            }
    

    The first index string in args is the path of your program.

    The second index string in args is the path of the file.

    Thanks for everyone helping me.