Saturday, April 23, 2011

Passing arguments to WPF application

Recentely I was working on a WPF application that can be used to open a file when the file is double clicked , so I had to pass the file name to the application. Here is how I did it by overridding OnStartup method in App.xaml.cs.

public partial class App : Application
{
   protected override void OnStartup(StartupEventArgs e)
   {
       if (e.Args != null && e.Args.Length > 0)
      {
           this.Properties["InputArgName"] = e.Args[0];
       }
       base.OnStartup(e);
   }
}
 
This can be access anywhere in the application using below code:
if (Application.Current.Properties["InputArgName"] != null)
{
   string fileName = Application.Current.Properties["InputArgName"].ToString();
}

No comments:

Post a Comment