2010-12-16 58 views
3

我試圖啓動一個基於開始菜單快捷方式的程序。特別是,我試圖基於其開始菜單快捷方式啓動powerpoint。爲了實現這一點,我使用了IWshRuntimeLibrary。無法從c中的開始菜單快捷方式啓動powerpoint

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

我能夠成功找到快捷方式以及創建IWshShortcut對象。但是,一旦我嘗試運行該快捷方式的應用程序的支持(通過的Process.Start(shortcut.TargetPath,shortcut.Arguments)),一個Win32Exception發生:

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: 

唯一相關答案我​​能找到在這個discussion。我的項目已經構建爲x86應用程序

由於它看起來相關,因此以下是我的系統的一些背景知識。我正在運行Windows 7 Professional 64位,並安裝了Microsoft Office 2010。這是在Visual Studio 2010中編寫的控制檯應用程序的一部分,其目標是.NET Framework 4 Client Profile。

我在調查以下的替代解決方案的過程:

在Windows API代碼包:http://code.msdn.microsoft.com/WindowsAPICodePack
快捷鍵無IWshRuntimeLibrary:How to resolve a .lnk in c#

很抱歉的缺乏理智的鏈接。這是我的第一篇文章,顯然我不能使用標籤插入多個帖子。

+0

你嘗試輸出'shortcut.TargetPath'的內容嗎? – mookid8000 2010-12-16 07:04:05

+0

@ mookid8000:我忘了將它包含在示例代碼中,我將它編輯。同時,這裏是我得到的:「C:\ Windows \ Installer \ {90140000-0011-0000-0000-0000000FF1CE} \ pptico.exe」。我應該注意到,powerpoint的確已安裝,許可和運作。 – Josh 2010-12-16 07:08:35

回答

2

這應該只是工作:

Process.Start(shortcutPath); 

你並不需要打開LNK,只問窗口來執行它。在現場後面,Process.Start將執行知道如何處理.lnk的ShellExecute

+0

這個效果很好。平時我做的事情太複雜了。謝謝。 – Josh 2010-12-16 07:14:30

相關問題