0

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

user1166085
  • 617
  • 3
  • 12
  • 23
  • Are you asking how to read the registry, or how to parse the path returned from reading it? – Rufus L Dec 03 '14 at 03:21
  • 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 file – user1166085 Dec 03 '14 at 03:25
  • 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 Dec 03 '14 at 03:29
  • sorry i was editing my comment, can you refer to my comment above? thanks in advance. :) – user1166085 Dec 03 '14 at 03:30
  • @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 Dec 03 '14 at 03:31
  • @HarryJohnston that's exactly the problem that i'm trying to overcome! thanks! :) so... any solution? – user1166085 Dec 03 '14 at 03:33
  • is there a c# equivalent method of it? i'm coding a winform application in c# – user1166085 Dec 03 '14 at 03:38
  • However, noting your recent edit: that isn't going to work as desired. Firstly, there's no guarantee that the program will accept the command-line syntax you're passing it, and secondly, to launch a new process you need the full path rather than the application name. – Harry Johnston Dec 03 '14 at 03:38
  • You should probably be using ShellExecuteEx or the .NET equivalent. – Harry Johnston Dec 03 '14 at 03:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66081/discussion-between-user1166085-and-harry-johnston). – user1166085 Dec 03 '14 at 03:41

3 Answers3

1

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
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Updated to include solution for getting full path. – Rufus L Dec 03 '14 at 04:09
  • hi! this seem to have worked for me for now. was testing around to try different extension and this seems to work just fine. oh! except that your method will leave a space behind "Skype.exe " which is easily resolved by using Replace. Thanks so much! :) – user1166085 Dec 03 '14 at 06:18
0

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.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • @RufusL: you can P/Invoke it, surely? OK, if there's a .NET equivalent that would be even better, but I don't know that there is. – Harry Johnston Dec 03 '14 at 03:29
  • An alternative approach would be to find the code in the C runtime library that parses the command line and port it to C#. (Again, not necessarily exactly the same as Explorer, mind you.) – Harry Johnston Dec 03 '14 at 03:58
0

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
regmagik
  • 574
  • 3
  • 14
  • it will only open local file, but however if you put in a url, it will proceed to download the file instead. :( – user1166085 Dec 03 '14 at 04:05
  • Could you please provide an example file URL you are trying to open? – regmagik Dec 03 '14 at 04:36
  • @user1166085 Thank you. I see that you want to open an intranet address to Word document. This works because MS Office knows how to open files from URL. – regmagik Dec 03 '14 at 06:26
  • 1
    But not all applications have the capability to open files from the URL. Have you considered downloading the file from the URL programmatically first, saving it to a local file and then using the Process.Start(localFile) method? There are samples of downloading file using powershell, for example http://stackoverflow.com/a/20476904/3220060. – regmagik Dec 03 '14 at 06:32
  • that is a problem that i have identified too. as the main intention of my application is to allow users to upload/download/edit document from a SharePoint document library, i would need to use the Process.Start for Office documents where it will allow users to directly make changes to the copy in the SharePoint document library. as for the other file types, i will probably allow user to save the file type like what you mentioned. thanks for pointing it out! – user1166085 Dec 03 '14 at 06:43