0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windowsのストアアプリを起動する

0
Posted at

起動したいんですよ。

カメラを起動したい

Windowsにはアプリがあるじゃないですか。
これをスタートメニューからだけでなく、自作アプリからも起動したいこと、ありますよね。
今回、C#+.NETで起動していきます。

起動する

もちろん、言わずと知れたProcess.Startを使います。
例えばVS2026を起動するなら、EXEのパスを調べて、以下のようなコードになります。

Process.Start(@"C:\Program Files\Microsoft Visual Studio\18\Community\Common7\IDE\devenv.exe");

しかし、冒頭の画像の「カメラ」アプリのようなストアアプリは、普通のアプリ、いわゆるEXEと同じようにはいきません。

// 一時的に起動はするけど良くない例
Process.Start(@"C:\Program Files\WindowsApps\Microsoft.WindowsCamera_2026.2605.7.0_x64__8wekyb3d8bbwe\WindowsCamera.exe");

普通のアプリと同じくタスクマネージャー等でパスを調べて入れれば一応は起動しますが、長持ちしません。

昔調べたパスでは起動しない

パスにバージョン番号らしきものが入っていることからもわかるとおり、自動アップデートでいつの間にか使えなくなります。

以下のように、explorer.exe経由で起動すると、アップデートしても正常に起動できます。

Process.Start(new ProcessStartInfo
{
    FileName = "explorer.exe",
    Arguments = @"shell:AppsFolder\Microsoft.WindowsCamera_8wekyb3d8bbwe!App",
    UseShellExecute = false,
});

「Microsoft.WindowsCamera_8wekyb3d8bbwe!App」の部分がAppIDです。
AppIDさえわかれば起動できるわけです。

もうちょっと一般化するとこうでしょうか。

var appId = @"Microsoft.WindowsCamera_8wekyb3d8bbwe!App";
Process.Start(new ProcessStartInfo
{
    FileName = "explorer.exe",
    Arguments = $@"shell:AppsFolder\{appId}",
    UseShellExecute = false,
});

スタートメニューにあるアプリなら、.exeで終わる普通のアプリも同じ方法で起動できるようです。

AppIDを取得する

AppIDを取得するには、PowerShellのGet-StartAppsコマンドを使います。

PS> Get-StartApps
(前略)
イベント ビューアー                                        Microsoft.AutoGenerated.{51C7ADDE-5601-5E65-E072-FD8558A034B3}
エクスプローラー                                           Microsoft.Windows.Explorer
カメラ                                                     Microsoft.WindowsCamera_8wekyb3d8bbwe!App
カレンダー                                                 microsoft.windowscommunicationsapps_8wekyb3d8bbwe!microsoft.windowslive.calendar
(後略)

こんな感じでずらずら出てきます。

フォルダ分けは考慮されていないようで、Uninstallとかのよくあるファイル名がやたらと出てきます。
このあたりどうにかする方法をご存じの方はお教えください。

ConvertTo-Jsonを使うと、もうちょっとプログラムから使いやすい形になります。

PS> Get-StartApps | ConvertTo-Json
[
  (前略)
  {
    "Name": "イベント ビューアー",
    "AppID": "Microsoft.AutoGenerated.{51C7ADDE-5601-5E65-E072-FD8558A034B3}"
  },
  {
    "Name": "エクスプローラー",
    "AppID": "Microsoft.Windows.Explorer"
  },
  {
    "Name": "カメラ",
    "AppID": "Microsoft.WindowsCamera_8wekyb3d8bbwe!App"
  },
  {
    "Name": "カレンダー",
    "AppID": "microsoft.windowscommunicationsapps_8wekyb3d8bbwe!microsoft.windowslive.calendar"
  },
  (後略
]

これをC#で書くと、こう。

var psi = new ProcessStartInfo
{
    FileName = "powershell.exe",
    Arguments = "-NoProfile -Command \"Get-StartApps | ConvertTo-Json\"",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true,
};
using var process = Process.Start(psi);
if (process == null)
{
    Console.Error.WriteLine("パワーシェル起動できなかったよー");
    return;
}
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

あとはまあ、JsonSerializerとかでパースすればどうとでも。

// StartAppEntryクラスを作ってる前提
var entries = JsonSerializer.Deserialize<StartAppEntry[]>(output);

ソースコード全文

Program.cs
using System.Diagnostics;
using System.Text.Json;

// PowerShellでアプリ一覧を出力
var psi = new ProcessStartInfo
{
    FileName = "powershell.exe",
    Arguments = "-NoProfile -Command \"Get-StartApps | ConvertTo-Json\"",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true,
};
using var process = Process.Start(psi);
if (process == null)
{
    Console.Error.WriteLine("パワーシェル起動できなかったよー");
    return;
}
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

// 返ってきたJSONをパース
var entries = JsonSerializer.Deserialize<StartAppEntry[]>(output);
if (entries == null)
{
    Console.Error.WriteLine("JSON読めなかったよー");
    return;
}

// 目的のアプリを探す
var name = "カメラ";
var entry = entries.FirstOrDefault(e => e.Name == name);
if (entry == null)
{
    Console.Error.WriteLine($"{name}見つからなかったよー");
    return;
}

// 起動
var appId = entry.AppID;
Process.Start(new ProcessStartInfo
{
    FileName = "explorer.exe",
    Arguments = $@"shell:AppsFolder\{appId}",
    UseShellExecute = false,
});
StartAppEntry.cs
public class StartAppEntry
{
    public string Name { get; set; } = string.Empty;
    public string AppID { get; set; } = string.Empty;
}

動作確認環境

  • Windows 11
  • PowerShell 7
  • Visual Studio 2026
  • .NET 10

リポジトリ

おまけ

カメラが見つかりません
我が家で「カメラ」アプリを起動した結果です。残念!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?