0
votes

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

3
Are you asking how to read the registry, or how to parse the path returned from reading it?Rufus L
I'm now able to read the registry and the value inside it. my question is when i get the value from the key, how am i able to get only the .exe part to get EXCEL.EXE , Skype.exe etc. 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 the fileuser1166085
So if you can already read the registry, is your question, "How can I get just the file name from a full file path?". Or something else?Rufus L
sorry i was editing my comment, can you refer to my comment above? thanks in advance. :)user1166085
@RufusL: it's a bit more complicated than that, because the string he's retrieving may contain command-line arguments as well as the path to the executable.Harry Johnston

3 Answers

1
votes

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
0
votes

In almost all cases, CommandLineToArgvW should work nicely. It returns an array of strings, the first element of which is the path to the executable.

Then all you need to do is remove the quote marks (if any) and everything up to the last backslash.

There may (or may not) be pathological cases in which this function does not interpret the string the same way as Explorer does. I don't think there's any way to determine this other than by experimentation.

0
votes

The comments to initial question clarify the actual problem.

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 the file

If all you need is to open a given file in associated application, you don't need to read the Registry, or parse the command line.

You can open file by calling a different overload of Process.Start that takes one parameter.

Process.Start(fileURL);

According to MSDN: http://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx, parameter is the name of a document or application file to run in the process.

I tested both local file, like "C:\folder\test.txt", and URLs.

Process.Start(@"C:\folder\test.txt"); // Opens test.txt in Notepad

and

Process.Start("http://regmagik.com/index.htm"); // Opens web page in the browser