Run an application via shortcut using Process.Start()
Asked Answered
B

5

15

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 );
Belton answered 26/5, 2011 at 16:22 Comment(4)
The shortcut requires arguments or is it the target of the shortcut that requires arguments?Giralda
@Giralda updated with requested info. The target of the shortcut requires arguments.Belton
Make sure the shortcut actually works - it could be broken.Magenta
@Charles the shortcut does work. It points to a bat file - I can run the bat file and the shortcut via point-and-click.Belton
B
18

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

Belton answered 26/5, 2011 at 17:26 Comment(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.Ingate
O
18

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

Process proc = new Process();
proc.StartInfo.FileName = @"c:\myShortcut.lnk";
proc.Start();
Oliguria answered 26/5, 2011 at 16:27 Comment(0)
B
18

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

Belton answered 26/5, 2011 at 17:26 Comment(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.Ingate
L
1

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");
Louanneloucks answered 29/4, 2014 at 7:27 Comment(0)
S
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.

Smitt answered 20/6, 2017 at 4:39 Comment(2)
Welcome to StackOverflow and thanks for helping. Taking the tour will get you your first badge. Have fun.Boylan
I don't understand. Why would you leave off the file extension?Reconciliatory
R
0

In my case, the problem was that if the application is designed for 32-bit processors, it cannot run the link file that refers to the application for 64-bit processors.

Reeve answered 19/4 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.