1

How can I open mp3 file with RealPlayer while the default is MediaPlayer

I know Process and ProcessStartInfo method, but I would like to know how to "open program with..."

Can you help me, plz?

  • 1
    If you want to show the `Open with...` dialog then see this answer: http://stackoverflow.com/questions/4726441/c-how-can-i-show-open-with#4726517. Otherwise, please let us know if you want to find `RealPlayer` and execute it programmatically with correct parameters et cetera. – Grant Thomas Jan 23 '11 at 15:03
  • thanks, Mr. Disappointment I want to open file in listBox automatically with realPlayer (without changing default program) –  Jan 23 '11 at 15:06
  • Then, prior to executing using `Process` and `ProcessStartInfo`, you ought to be asking/figuring: `How do I determine if RealPlayer is installed?` Which will, in turn, leave you with all the required periphery information to use the former mentioned types to achieve your goal, simply. – Grant Thomas Jan 23 '11 at 15:54
  • 1
    Find out where realplayer is installed and then run it with your file as argument. Just like Mr. Disappointment recommended in his first comment. – CodesInChaos Jan 23 '11 at 16:17
  • Can you tell me how can I run it as argument? real player path is C:\Program Files (x86)\Real\RealPlayer\realplay.exe –  Jan 23 '11 at 18:29

1 Answers1

1

Okay, so thought I'd make this possible for you before I clock off for the night. I have thrown together a working console application which loads (known) installed programs from the registry's App Path key. The solution is far from perfect, won't be the safest, fastest, or most reliable solution, and it certainly shouldn't be seen amongst any production code, but it is more than enough to aid you, hopefully, in developing what it is you need:

So, here is the code, minus the namespace...

using System;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length >= 0 && !string.IsNullOrEmpty(args[0]) && File.Exists(args[0]))
        {
            var programs = new InstalledPrograms();
            var programKey = "RealPlay.exe".ToLowerInvariant();
            if (programs.ContainsKey(programKey))
            {
                var programPath = programs[programKey];
                if (!string.IsNullOrEmpty(programPath) && File.Exists(programPath))
                {
                    var process = new Process();
                    process.StartInfo = new ProcessStartInfo(programPath);
                    process.StartInfo.Arguments = args[0];
                    if (process.Start())
                    {
                        Console.WriteLine("That was easy!");
                    }
                    else
                    {
                        Console.WriteLine("Hell's bells and buckets of blood, we seem to have hit a snag!");
                    }
                }
            }
        }
        else
        {
            Console.WriteLine("Specify a file as an argument, silly!");
        }
        Console.WriteLine();
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    class InstalledPrograms : Dictionary<string, string>
    {
        static string PathKeyName = "Path";
        static string RegistryKeyToAppPaths = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";           

        public InstalledPrograms()
        {
            Refresh();
        }

        public void Refresh()
        {
            Clear();
            using (var registryKey = Registry.LocalMachine.OpenSubKey(RegistryKeyToAppPaths))
            {
                var executableFullPath = string.Empty;
                foreach (var registrySubKeyName in registryKey.GetSubKeyNames())
                {
                    using (var registrySubKey = registryKey.OpenSubKey(registrySubKeyName))
                    {
                        executableFullPath = registrySubKey.GetValue(string.Empty) as string;
                        Add(registrySubKeyName.ToLowerInvariant(), executableFullPath);
                    }
                }
            }
        }
    }
}

Though we check for file existence, and other minor but necessary checks are made, you would still need to tighten this up further when plugged into the environment of your own code, including, among other things, exception handling for, but not limited to, registry access issues.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • I am very happy with your great answer and actually I don't know how can I thank you -------- but can you tell me please: when I start program an error appear "Index was outside the array" –  Jan 24 '11 at 16:13
  • 1
    You need to run the program with at least one argument, and that argument should be a valid path to a file (i.e. one RealPlayer recognises) - we don't just randomly find a file to open. – Grant Thomas Jan 24 '11 at 16:51