简体   繁体   English

无法从c#中的开始菜单快捷方式启动powerpoint

[英]Can't start powerpoint from start menu shortcut in c#

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. 特别是,我正在尝试基于其开始菜单快捷方式启动powerpoint。 To accomplish this, I am using the IWshRuntimeLibrary. 为此,我使用的是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. 我能够成功找到快捷方式以及创建IWshShortcut对象。 However, once I attempt to run the shortcut's backing application (via Process.Start(shortcut.TargetPath, shortcut.Arguments)), a Win32Exception occurs: 但是,一旦我尝试运行快捷方式的后备应用程序(通过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:

The only related answer I could find was in this discussion . 我能找到的唯一相关答案就是在这个讨论中 My project was already building as an x86 application 我的项目已经构建为x86应用程序

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. 我正在运行Windows 7 Professional 64位,并安装了Microsoft Office 2010。 This is part of a console application written in Visual Studio 2010, targeting .NET Framework 4 Client Profile. 这是用Visual Studio 2010编写的控制台应用程序的一部分,目标是.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 Windows API代码包: http//code.msdn.microsoft.com/WindowsAPICodePack
Shortcuts without IWshRuntimeLibrary: How to resolve a .lnk in c# 没有IWshRuntimeLibrary的快捷方式: 如何在c#中解析.lnk

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. 你不需要打开lnk,只需要求windows执行它。 Behind the scene, Process.Start will do a ShellExecute that knows how to process a .lnk. 在场景后面,Process.Start将执行一个知道如何处理.lnk的ShellExecute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM