Jayashan Lakshitha
Jayashan Lakshitha

Reputation: 15

Open custom files with own C# application

I'm creating a media player.

So far I can open a file type with my C# application by double clicking on the file. But I want to open multiple files by selecting them and opening them at once..

My code goes as follows:

App.xmal.cs

protected override void OnStartup(StartupEventArgs e)
{    
    if (e.Args != null && e.Args.Length > 0)
    {
        this.Properties["ArbitraryArgName"] = e.Args[0];
    }

    base.OnStartup(e);
}

MainWindow.xmal.cs

if (Application.Current.Properties["ArbitraryArgName"] != null)
{
    string fname = Application.Current.Properties["ArbitraryArgName"].ToString();           

    if (fname.EndsWith(".mp3") || fname.EndsWith(".wma") || fname.EndsWith(".wav") || fname.EndsWith(".mpg") || 
        fname.EndsWith(".mpeg") || fname.EndsWith(".mp4") || fname.EndsWith(".wmv") )
    {
        doubleclicktrack(fname);
    }
    else
    {
         this.Close();
    }
}

This code works fine with one file, but how to change this in order to open multiple files at once by selecting multiple files and opening them at once (by pressing enter key).

Upvotes: 1

Views: 431

Answers (2)

MarioDS
MarioDS

Reputation: 13063

You will have to look into developing shell extensions in order to achieve what you want. Just by using the registry to associate file types with your app, you are limited to passing just one file per command, which will end up opening your app once per file (which you have confirmed).

I guess you could also implement your app to allow only one instance running globally. That way, whenever a command comes in to open one more file, you could for example add it to a playlist or something.

Note that shell extensions have to be written in C++, Microsoft strongly advises to avoid managed code for this purpose.

You can find the documentation starting point here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776778(v=vs.85).aspx

Upvotes: 1

cyanide
cyanide

Reputation: 3964

FileOpenPicker can do that (Code for UWP or Windows 8.1 desktop, with Windows 8.1 phone it's a bit trickier):

private static readonly string[] FileTypes =  {
   "aac", "adt", "adts", "mp3", "m3a", "m4a", "mpr", "3gp", "3g2",
   "flac", "wav", "wma"  };

...........

FileOpenPicker picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
foreach (String fileType in FileTypes)
     picker.FileTypeFilter.Add("." + fileType);

var list = await picker.PickMultipleFilesAsync();

FYI Your manifest must declare "Music Library" in Capabilities

Upvotes: 0

Related Questions