user664939
user664939

Reputation: 2055

Run an application via shortcut using Process.Start()

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 );

Upvotes: 16

Views: 41779

Answers (5)

pekand
pekand

Reputation: 21

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.

Upvotes: 0

HippyPotamus
HippyPotamus

Reputation: 11

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.

Upvotes: 1

Rezi R.
Rezi R.

Reputation: 41

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");

Upvotes: 1

user664939
user664939

Reputation: 2055

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

Upvotes: 18

keyboardP
keyboardP

Reputation: 69372

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

Process proc = new Process();
proc.StartInfo.FileName = @"c:\myShortcut.lnk";
proc.Start();

Upvotes: 18

Related Questions