0

I have been trying to launch Microsoft Teams through C#, by launching the shortcut.

However I am getting an error:

System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'

This is my code:

using System;
using System.Diagnostics;

namespace OnlineLearning
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo teams = new ProcessStartInfo(@"C:\Users\cesq\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk");
            teams.CreateNoWindow = true;
            teams.RedirectStandardError = true;
            teams.RedirectStandardOutput = true;
            teams.RedirectStandardInput = true;
            Process foo = Process.Start(teams);
        }
    }
}

I have been following an example from this question: Run an application via shortcut using Process.Start()

And did as one answer suggested:

        Process proc = new Process();
        proc.StartInfo.FileName = @"C:\Users\cesq\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk";
        proc.Start();

Sadly this didn't work (gave the same error).

Also the accepted answer:

Setting UseShellExecute = false was the problem. Once I removed that, it stopped crashing.

Didn't work for me, since I already have done what it suggested.

Also this question: How to run a shortcut did not help me as well, as it is not relevant to my example.

As well as this one: 'The specified executable is not a valid application for this OS platform

Now I am out of ideas, so I am asking for your help.

  • Did you see the comment on that answer? *"This is even more important now because .NET core has changed the default of this property to false. I was running into this when I wasn't setting this property at all. – Cdaragorn"* Looks like you need to set it to `true`, whereas the solution was to leave it as its default, which is different now. – madreflection Dec 28 '21 at 16:06
  • @madreflection Did like you suggested, gives `System.InvalidOperationException: 'The Process object must have the UseShellExecute property set to false in order to redirect IO streams.'` – GetTwoBirdsStonedAtOnce Dec 28 '21 at 16:09
  • Do you *really* need to redirect the IO streams? Highly doubtful. Don't set those to true. – madreflection Dec 28 '21 at 16:10
  • @madreflection, Ok I googled the error and found an answer. Needed to set it to true, but also to set `teams.RedirectStandardOutput` to `false` and remove `teams.RedirectStandardInput` and `teams.RedirectStandardError` . – GetTwoBirdsStonedAtOnce Dec 28 '21 at 16:14
  • 1
    I don't think you need to set them at all. Redirection of IO streams is opt-in. – madreflection Dec 28 '21 at 16:15
  • @madreflection You are correct, thank you. – GetTwoBirdsStonedAtOnce Dec 28 '21 at 16:17
  • Does this answer your question? [Run an application via shortcut using Process.Start()](https://stackoverflow.com/questions/6141821/run-an-application-via-shortcut-using-process-start) – madreflection Dec 28 '21 at 16:18
  • I've flagged it as a duplicate of the question you linked because the answer was there all along. – madreflection Dec 28 '21 at 16:18

0 Answers0