0

I have problem with passing arguments into my program. Example: there are some mp3 files in some folder. My program is set to be a default program. If I select only one file and double click it, everything works fine. The problem is with more than one selected file. When i select more files and enter them, it starts its onw instance of program for each selected file. Is there any possibility to pass them as string array via command line args?

Edit:

run only one instance of my program I use

        Process[] proc = Process.GetProcessesByName("MKMusicPlayer");
        if (proc.Length > 1)
        {
            MessageBox.Show("Program is already running!");
            proc[1].Kill();

        }

and to get args

            if (Environment.GetCommandLineArgs().Length > 1)
            {//code}
mmaverikk
  • 223
  • 1
  • 12
  • 5
    That's how windows works. You'd have to make your application detect that an instance is already running and pass the argument to the running instance to achieve what you want. – juharr Mar 12 '15 at 13:07
  • In addition to the code how are you selecting files? – kmcnamee Mar 12 '15 at 13:07
  • possible duplicate of [how to send 2-3 param's to Winform C# program?](http://stackoverflow.com/questions/2871749/how-to-send-2-3-params-to-winform-c-sharp-program) – Xaruth Mar 12 '15 at 13:12
  • 4
    As far as I know a new default application instance will be run for each file you try to open, so I could only recommend you to read http://stackoverflow.com/questions/819773/run-single-instance-of-an-application-using-mutex and http://stackoverflow.com/questions/10390829/is-it-possible-to-talk-with-running-process to allow only single instance that gets new file paths to process from its other execution attempts. – Eugene Podskal Mar 12 '15 at 13:13
  • @juharr: it is already implemented. It detects running app with the same name and protect it to run again. – mmaverikk Mar 12 '15 at 13:18
  • @kmcnamee: e.g. user select all files in folder and press enter, it runs my program for each selected file instead of passing them as args. – mmaverikk Mar 12 '15 at 13:20
  • 2
    @mmaverikk In that case you should show your code for making your application single-instance. Also if you open one file and then another does it correctly pass the second file to the first instance and not open a second instance? – juharr Mar 12 '15 at 13:23
  • @juharr it always tries to open new instance. So how to prevent this behaving? How to pass this args to my running app? – mmaverikk Mar 12 '15 at 13:40
  • 1
    @mmaverikk You can use a Mutex for making it single instance and Named Pipes for communication. Check out the links in Eugene's comment. – juharr Mar 12 '15 at 13:48
  • @mmaverikk I have already given to you [links](http://stackoverflow.com/questions/29010537/c-sharp-pass-more-file-paths-via-command-line-args#comment46266478_29010537) to the required information that will allow you to create a working solution. It is neither pretty nor simple, but short of changing how OS handles default program it is probably the only real option. Perhaps there is already some finished solution that uses such approach but you will have to search for it. – Eugene Podskal Mar 12 '15 at 13:49

0 Answers0