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

发布于 2024-10-08 05:24:21 字数 3028 浏览 0 评论 0原文

我正在尝试根据开始菜单快捷方式启动程序。特别是,我尝试根据其开始菜单快捷方式启动 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)),就会发生 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:

我能找到的唯一相关答案是在此 讨论。我的项目已经构建为 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 的快捷方式: 如何在 c# 中解析 .lnk

很抱歉缺少合理的链接。这是我的第一篇文章,显然我无法使用标签插入多个帖子。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2024-10-15 05:24:21

这应该很简单:

Process.Start(shortcutPath);

您不需要打开 lnk,只需要求 Windows 执行它即可。在幕后,Process.Start 将执行知道如何处理 .lnk 的 ShellExecute

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文