Wie starte ich Dateien in C #

Ich fühle mich wie ein Idiot. Ich hatte das Gefühl, dass etwas wie die unten stehende Antwort funktionieren würde, konnte jedoch keine Google-Ergebnisse sehen, die den unten stehenden Antworten ähneln. Als ich diesen komplexen Code sah, dachte ich, dass es so sein musste.

Ich habe das gesucht und gefundenWindows: Mit einer Erweiterung verknüpfte Anwendungen auflisten und starten Meine Frage wurde jedoch nicht beantwortet. Mit Tweaks unten habe ich mir das unten ausgedacht. Es bleibt jedoch bei Bilddateien hängen. Txt-Dateien laufen einwandfrei

Ich werde diesen Code in Kürze aktualisieren, um App-Pfade mit Leerzeichen zu berücksichtigen. Ich verstehe jedoch nicht, warum Bilddateien nicht gestartet werden.

static void launchFile(string fn)
{
    //majority was taken from
    //https://stackoverflow.com/questions/24954/windows-list-and-launch-applications-associated-with-an-extension
    const string extPathTemplate = @"HKEY_CLASSES_ROOT\{0}";
    const string cmdPathTemplate = @"HKEY_CLASSES_ROOT\{0}\shell\open\command";

    string ext = Path.GetExtension(fn);

    var extPath = string.Format(extPathTemplate, ext);

    var docName = Registry.GetValue(extPath, string.Empty, string.Empty) as string;
    if (!string.IsNullOrEmpty(docName))
    {
        // 2. Find out which command is associated with our extension
        var associatedCmdPath = string.Format(cmdPathTemplate, docName);
        var associatedCmd = Registry.GetValue(associatedCmdPath, string.Empty, string.Empty) as string;

        if (!string.IsNullOrEmpty(associatedCmd))
        {
            //Console.WriteLine("\"{0}\" command is associated with {1} extension", associatedCmd, ext);
            var p = new Process();
            p.StartInfo.FileName = associatedCmd.Split(' ')[0];
            string s2 = associatedCmd.Substring(p.StartInfo.FileName.Length + 1);
            s2 = s2.Replace("%1", string.Format("\"{0}\"", fn));
            p.StartInfo.Arguments = s2;//string.Format("\"{0}\"", fn);
            p.Start();
        }
    }
}

Antworten auf die Frage(3)

Ihre Antwort auf die Frage