Search code examples
c#windowsregistry

how do i get the application name from the default value in Registry/LocalMachine/Software/Classes/.docx/shell/Open/Command


i would need to know the most effective way get the name of the application from the Registry/LocalMachine/Software/Classes/.docx/shell/Open/Command . for example from this

"C:\Program Files (x86)\Microsoft Office\Office15\POWERPNT.EXE" "%1" /ou "%u"

i would need only the 'POWERPNT.EXE'. substring and replace is not effective as the value inside appears differently. for example

"C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE" /dde

"C:\Program Files (x86)\Skype\Phone\Skype.exe" "/uri:%l"

the real problem that i'm encountering here is that the string that i'm retrieving may contain command-line arguments as well as the path to the executable for which substring and replace would not be helpful anymore.

the intention of my method is to find out the program being used to open associated file type and then using the Process.Start("EXCEL.EXE", fileURL) to open a file from a SharePoint DocumentLibrary


Solution

  • Would something like this work?

    public static string GetFileName(string input)
    {
        int extensionIndex = input.IndexOf(".exe", StringComparison.OrdinalIgnoreCase);
        if (extensionIndex < 0) return string.Empty;
        return Path.GetFileName(input.Replace("\"", "").Substring(0, extensionIndex + 4));
    }
    
    // Or, if you want to get the full path:
    public static string GetFilePath(string input)
    {
        int extensionIndex = input.IndexOf(".exe", StringComparison.OrdinalIgnoreCase);
        if (extensionIndex < 0) return string.Empty;
        return input.Replace("\"", "").Substring(0, extensionIndex + 4);
    }
    

    Usage:

    string regValue =
        "C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE /dde";
    
    Console.WriteLine(GetFileName(regValue));
    Console.WriteLine(GetFilePath(regValue));
    // Output:
    // EXCEL.EXE
    // C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE
    
    regValue = "\"C:\\Program Files (x86)\\Skype\\Phone\\Skype.exe\" \"/uri:%l\"";
    
    Console.WriteLine(GetFileName(regValue));
    Console.WriteLine(GetFilePath(regValue));
    // Output:
    // Skype.exe
    // C:\Program Files (x86)\Skype\Phone\Skype.exe