0

I've Created a Music Player using Visual Studio 2012 Windows Form C#. now I want the user to be able to play songs in windows explorer such as other players(Windows Media Player,Winamp,etc.) with my player. I already found the way to File association!

but I need to prevent my application from running multiple instance(as WMP&... don't) and I want to get the songs Paths too to send them to my application(already started).

For example User Select 3 Songs in a directory in windows explorer & Press Enter so my application / and execute my AddFiles Function (add the supported files to playlist and ...)

I tried mutex it solves the first part(just single instance) but can't get arguments from it!

I also tried this but No chance :( it gave error!

** I already triedWhat is the correct way to create a single instance application? "Matt Davis" Answer and it makes my application to be single instance only and bring to front part was awesome but didn't send arguments to my running process so It couldn't solve my problem!

any Help would be in advance :)

UPDATE: I don't understand while I haven't get my problem solved why experts close the question!? :| :/

UPDATE 2 (FOUND THE SOLUTION):

ok Finally I've got the solution :)

this link helped me to get paths of selected files in explorer by clicking on a context-menu item: .NET Shell Extensions - Shell Context Menus

real easy :) Hope this help others too!

Community
  • 1
  • 1
ACE
  • 379
  • 3
  • 12
  • as I said I already tried that article! It does the half of job i want! but does not solve the arguments part(file paths) – ACE Mar 16 '15 at 15:16
  • or maybe I didn't use it correctly! – ACE Mar 16 '15 at 15:16
  • I tried "Matt Davis" Answer its bringing to front window part is awesome i like that! but as I said i couldn't get all selected filespaths by it! :( – ACE Mar 16 '15 at 15:20
  • 2
    "maybe I didn't use it correctly" -- the answer from Matt Davis doesn't support parameter passing. But this one from the same question does: http://stackoverflow.com/a/19326/3538012 There are lots of other ways to deal with the parameter passing, but you'll have to implement _some_ mechanism. The `Main()` method of your original instance is not going to get called again, so to pass command line args to the program once it's started requires additional effort on your part. – Peter Duniho Mar 16 '15 at 15:45
  • Thanks @PeterDuniho I'm gonna try that answer too! but maybe not now I'm really sleepy & want to sleep after waking up give it a try! hope it solves my problem – ACE Mar 16 '15 at 15:48
  • it was giving error about mainwindow! i changed it a little and it's gone but another error is about "SingleInstanceApplication" Errors: 1) Error 1 The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. D:\...\WindowsFormsApplication61\Program.cs 10 18 WindowsFormsApplication61 – ACE Mar 16 '15 at 16:13
  • 2) Error 2 The type 'System.Windows.Threading.DispatcherObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. D:\...\WindowsFormsApplication61\Program.cs 10 18 WindowsFormsApplication61 – ACE Mar 16 '15 at 16:13
  • as my question closed I can't answer it but I found the solution! with this [link](http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus) I can get filepaths so I can use these paths to add those songs to my playlist! – ACE Mar 17 '15 at 14:02

1 Answers1

1

I use this: https://code.msdn.microsoft.com/windowsapps/CSWinFormSingleInstanceApp-d1791628

It's single instance, and supports command line args. My program is started like this:

[STAThread]
static void Main(String [] args) {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Form1 mf = new Form1(); // your form
    SingleInstanceAppStarter.Start(mf, StartNewInstance);
}

...

private static void StartNewInstance(object sender, StartupNextInstanceEventArgs e) {
    String cmdArg = e.CommandLine[1]; // yes, 1 to get the first.  not zero.
    ...
}

You'll also need this:

class SingleInstanceAppStarter
{
    static SingleInstanceApp app = null;

    public static void Start(Form f, StartupNextInstanceEventHandler handler)
    {
        if (app == null && f != null)
        {
            app = new SingleInstanceApp(f);
        }
        app.StartupNextInstance += handler;
        app.Run(Environment.GetCommandLineArgs());
    }
}

and this:

class SingleInstanceApp : WindowsFormsApplicationBase
{
    public SingleInstanceApp() { }

    public SingleInstanceApp(Form f)
    {
        base.IsSingleInstance = true;
        this.MainForm = f;
    }
}

Note that both of those classes use the Microsoft.VisualBasic.ApplicationServices assembly (You'll have to reference it).

Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • I tried it & it shows an error while compiling: Win32 Exception was unhadled Error creating window handle. – ACE Mar 16 '15 at 15:52