通过非默认程序C sharp打开mp3

时间:2011-01-23 14:47:23

标签: c#

如何使用RealPlayer打开mp3文件,默认为MediaPlayer

我知道Process和ProcessStartInfo方法,但我想知道如何用#...打开程序"

你能帮我吗,PLZ?

1 个答案:

答案 0 :(得分:1)

好的,所以我想在晚上休息之前让这件事成为可能。我把一个工作的控制台应用程序放在一起,该应用程序从注册表的App Path密钥加载(已知)已安装的程序。解决方案远非完美,不是最安全,最快速或最可靠的解决方案,而且在任何生产代码中都不应该看到它,但它足以帮助您,希望在开发它的过程中你需要:

所以,这是代码,减去命名空间......

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);
                    }
                }
            }
        }
    }
}

虽然我们检查文件存在,并且进行了其他次要但必要的检查,但是当插入到您自己的代码的环境中时,您仍然需要进一步加强这一点,包括(除其他外)异常处理,但不是仅限于注册管理机构访问问题。