-1

I need to start the default mail client using the command line received from windows registry. How to do it in C# ? Process.Start cannot execute the whole line, it needs to be split, but i dont known what it will be

I get a line to run for example here in registry

HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\Microsoft Outlook\shell\open\command

How to run this command line with c# ?

"C:\PROGRA~2\MICROS~1\Office16\OUTLOOK.EXE" /recycle

More complex example

%systemRoot%\system32\rundll32.exe "%ProgramFiles%\Internet Explorer\hmmapi.dll",OpenInboxHandler
AlexanderT
  • 79
  • 1
  • 8

2 Answers2

0

Did so

string[] lines = cmd.Split('"');
if (lines.Length > 1)
{
    ProcessStartInfo process = new ProcessStartInfo(lines[1].Trim());
    process.UseShellExecute = true;
    if (lines.Length > 2)
        process.Arguments = lines[2].Trim();
    Process.Start(process);
}

but don't eat it

%systemRoot%\system32\rundll32.exe "%ProgramFiles%\Internet Explorer\hmmapi.dll",OpenInboxHandler
AlexanderT
  • 79
  • 1
  • 8
  • 1
    If the command line has quotes in the arguments, this will not work. It works for the Outlook command line you found, but you can't be certain that some other client that's been set as the default won't have a quote in the arguments. What Reza Aghaei suggested is a more robust way to do it. – madreflection Aug 21 '19 at 19:27
  • ^Not to mention this will also grab what is in between the double quotes (`"`) , so `lines[2].Trim()` will most likely be `string.Empty` – Jaskier Aug 21 '19 at 19:29
0

The issue is resolved! To execute an arbitrary shell command, you need to specify the /C parameter for cmd.exe

public static void ExecuteShellCommand(string command)
{
    var ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + command)
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        CreateNoWindow = true,
        UseShellExecute = true
    };
    Process.Start(ProcessInfo);
}
AlexanderT
  • 79
  • 1
  • 8