質問

スタートメニューショートカットに基づいてプログラムを開始しようとしています。特に、スタートメニューのショートカットに基づいて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);
}

ショートカットを正常に見つけることができ、iwshortcutオブジェクトを作成することができます。ただし、ショートカットのバッキングアプリケーションを実行しようとすると(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フレームワーク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