14

Is there a way to run an application via shortcut from a C# application?

I am attempting to run a .lnk from my C# application. The shortcut contains a significant number of arguments that I would prefer the application not have to remember.

Attempting to run a shortcut via Process.Start() causes an exception.

Win32Exception: The specified executable is not a valid Win32 application

This is the code I am using.

ProcessStartInfo info = new ProcessStartInfo ( "example.lnk" );
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
Process whatever = Process.Start( info );
apaderno
  • 26,733
  • 16
  • 74
  • 87
user664939
  • 1,887
  • 2
  • 20
  • 34

4 Answers4

18

Could you post some code. Something like this should work:

Process proc = new Process();
proc.StartInfo.FileName = @"c:\myShortcut.lnk";
proc.Start();
keyboardP
  • 67,723
  • 13
  • 149
  • 201
13

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

user664939
  • 1,887
  • 2
  • 20
  • 34
  • 1
    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 Sep 10 '19 at 16:53
1

If you're using UseShellExecute = false and trying to launch a batch file make sure to add .bat to the end of the filename. You don't need .bat if UseShellExecute = true though. This made me just waste an hour of work... hoping to save someone else.

0

if your file is EXE or another file type like ".exe" or ".mkv" or ".pdf" and you want run that with shortcut link your code must like this.

i want run "Translator.exe" program.

Process.Start(@"C:\Users\alireza\Desktop\Translator.exe.lnk");
Alireza
  • 31
  • 4