我正在尝试根据开始菜单快捷方式启动程序。特别是,我正在尝试根据其开始菜单快捷方式启动PowerPoint。为此,我正在使用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);
}

我能够成功找到快捷方式以及创建一个IWSHSHORTCUT对象。但是,一旦我尝试运行快捷方式的备份应用程序(通过Process.start(shortcut.targetpath,shortcut.arguments)),会发生win32 exception:

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:

我能找到的唯一相关答案是 讨论. 。我的项目已经作为X86应用程序构建

由于它似乎很重要,因此这里有一些背景。我正在运行Windows 7 Professional 64位,并安装了Microsoft Office 2010。这是Visual Studio 2010中编写的控制台应用程序的一部分,针对.NET Framework 4客户端配置文件。

我正在研究以下替代解决方案:

Windows API代码包: http://code.msdn.microsoft.com/windowsapicodepack
没有IWSHRUNTIMELIBRARY的快捷方式: 如何在C#中解析.lnk

对不起,缺乏明智的链接。这是我的第一篇文章,显然我无法使用标签插入多个。

有帮助吗?

解决方案

这应该简单地工作:

Process.Start(shortcutPath);

您无需打开LNK,只需要求Windows执行它即可。在现场,process.start将做一个 Shellexecute 知道如何处理.lnk。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top