0

I want to implement a wpf application that will listen to an event that comes from the desktop from the Send To shortcut. Such as right click on the file and select send to app, then get the file path.

How would that be developed?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
galbru
  • 422
  • 1
  • 5
  • 15

1 Answers1

2

SendTo resolves the link in the %APPDATA%\Microsoft\Windows\SendTo folder and passes the filename as a parameter to the proper executable. You need to make your program accept command parameters and then process them.

EDIT: I originally missed the mention of WPF. So you could process command line args like this.

In you App.xaml add an entry for Startup like this:

<Application x:Class="WpfApplication4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="App_OnStartup"
             StartupUri="MainWindow.xaml">
  <Application.Resources />
</Application>

In your App.xaml.cs add the App_OnStartup like this and store the args into an accessible variable:

namespace WpfApplication4
{
  /// <summary>
  /// Interaction logic for App.xaml
  /// </summary>
  public partial class App : Application
  {

      public static string[] mArgs;

      private void App_OnStartup(object sender, StartupEventArgs e)
      {
          if (e.Args.Length > 0)
          {
              mArgs = e.Args;
          }
      }
    }
}

In your main window get the args and do something with it:

namespace WpfApplication4
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      string[] args = App.mArgs;

      //do your procedure with the args!
    }
  }
}

Then place a shortcut to your program in the %APPDATA%\Microsoft\Windows\SendTo folder. When you right click on a file and SendTo your app, the filename will be the args that are passed into your app.

trashrobber
  • 727
  • 2
  • 9
  • 26
  • Where should I place the code? I'm working with WPF... – galbru Aug 12 '13 at 20:26
  • @Mr.Bean Override the `OnStartup` in your `App` class. The `StartupEventArgs` contains a collection of the command line parameters. – ChrisWue Aug 12 '13 at 21:13
  • Thank you. It was very helpful. Do you know how to do it if the app already running? – galbru Aug 13 '13 at 08:16
  • @Mr. Bean. It gets a little more tricky if you want it to be a single instance app that takes further command line arguments... here is a good article on how to accomplish that. [http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx](http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx) – trashrobber Aug 13 '13 at 13:13