I'm attempting to start a program based on a start menu shortcut. In particular, I'm trying to launch powerpoint based on its start menu shortcut. To accomplish this, I am using the IWshRuntimeLibrary.
static void Main(string[] args)
{
string searchTerm = "powerpoint";
string startMenu = Environment
.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
string shortcutPath = Directory
.GetFiles(startMenu, "*.lnk", SearchOption.AllDirectories)
.OrderBy(p => p.Length)
.First(p => p.ToLower().Contains(searchTerm));
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
Console.WriteLine(shortcut.TargetPath); //prints: C:\Windows\Installer\{90140000-0011-0000-0000-0000000FF1CE}\pptico.exe
Process.Start(shortcut.TargetPath, shortcut.Arguments);
}
I am able to successfully find the shortcut as well as create an IWshShortcut object. However, once I attempt to run the shortcut's backing application (via Process.Start(shortcut.TargetPath, shortcut.Arguments)), a Win32Exception occurs:
System.ComponentModel.Win32Exception was unhandled
Message=The specified executable is not a valid application for this OS platform.
Source=System
ErrorCode=-2147467259
NativeErrorCode=193
StackTrace:
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName, String arguments)
at Scratch.Program.Main(String[] args) in C:\Users\jhampton\Documents\Visual Studio 2010\Projects\Scratch\Scratch\Program.cs:line 26
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The only related answer I could find was in this discussion. My project was already building as an x86 application
Since it seems relevant, here's some background on my system. I am running Windows 7 Professional 64-Bit, and have Microsoft Office 2010 installed. This is part of a console application written in Visual Studio 2010, targeting .NET Framework 4 Client Profile.
I'm in the process of investigating the following alternative solutions:
The Windows API Code Pack: http://code.msdn.microsoft.com/WindowsAPICodePack
Shortcuts without IWshRuntimeLibrary: How to resolve a .lnk in c#
Sorry about the lack of sensible links. It's my first post and apparently I can't insert more than one using a tags.
This should simply work:
Process.Start(shortcutPath);
You don't need to open the lnk, just ask windows to execute it. Behind the scene, Process.Start will do a ShellExecute that knows how to process a .lnk.