简体   繁体   中英

C# WPF StartupEvent get path

My objective is that when I open a file with a specific extension it will open my program in C#. That part is done but now I want to get the path of the file that I clicked on previously.

This is just the main code of App.xaml.cs :

/// <summary>
/// Logique d'interaction pour App.xaml
/// </summary>
public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {

    }
}

Here are my two commands to associate my extension to my program.

    ftype GDevDB=N:\Divers\GDevDB\bin\Debug\GDevDB.exe "%1"
    assoc .GDevDB=GDevDB

Solution :

I've put the command in a batch file and the %1 was the one of the batch file and it was returning an empty string value.

The file you clicked on will be in the first command line argument. The arguments are in the array of strings e.Args , in the StartupEventArgs . For the file you clicked on, you thus need to get e.Args[0] .

You can also use Environment.GetCommandLineArgs() If you need those somewhere else. Index 0 is the path to your program. Index 1 is the file you are looking for

Try to do this:

 public partial class App : Application
        {
            private void Application_Startup(object sender, StartupEventArgs e)
            {
                foreach (string arg in e.Args)
                {
                    //Do something
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM