【问题标题】:Control path to pinned exe in Windows taskbar and start menuWindows 任务栏和开始菜单中固定 exe 的控制路径
【发布时间】:2019-08-28 06:22:15
【问题描述】:

我正在使用一对程序,“starter”(更新程序)和“main”程序。 starter 在哪里更新和启动 main。

程序在固定到 Windows 任务栏或开始菜单时必须表现良好。 例如,用户应该能够:

  1. 启动“启动器”
  2. 将正在运行的“main”固定到任务栏
  3. 关闭程序
  4. 使用任务栏上的固定项目启动程序。

是否可以将固定的快捷方式直接指向“starter”而不是“main”?

我试过group them using a common ID,但这并不影响被固定的路径。

现在我正在“starter”进程中加载​​“main”。 这可以按预期工作,但问题是所有更新都受到“starter”的 .NET 版本的限制,该版本现在已经很老了,.NET 3.5。

【问题讨论】:

标签: c# taskbar


【解决方案1】:

System.AppUserModel 设置以下属性。

  • System.AppUserModel.ID
  • System.AppUserModel.RelaunchCommand
  • System.AppUserModel.RelaunchDisplayNameResource

在 C# 中,您可以使用 Windows-API-Code-Pack 或其 NuGet 包 WindowsAPICodePack-Shell

注意unknown reason you can't easily change the path once it's set

void SetTaskbarRelaunchCommand(Form form)
{
    // WARNING, once RelaunchCommand has been set it can't be changed for any given appID.
    // Workaround: delete all links here related to our app.
    // %AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\ImplicitAppShortcuts
    // %AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
    // Source: https://stackoverflow.com/a/28388958/33236

    var appID = "MyAppID";
    var path = @"C:\Program Files (x86)\MyApp\Updater.exe");
    var handle = form.Handle;

    var propGuid = new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}");
    var ID = new PropertyKey(propGuid, 5);                          // System.AppUserModel.ID
    var RelaunchCommand = new PropertyKey(propGuid, 2);             // System.AppUserModel.RelaunchCommand
    var RelaunchDisplayNameResource = new PropertyKey(propGuid, 4); // System.AppUserModel.RelaunchDisplayNameResource

    WindowProperties.SetWindowProperty(handle, ID, appID);
    WindowProperties.SetWindowProperty(handle, RelaunchCommand, path);
    WindowProperties.SetWindowProperty(handle, RelaunchDisplayNameResource, "Label of My App");
}

您还可以完全阻止应用固定。 作为opposed to RelaunchCommand,您可以随时更改此值。

void PreventPinning(IntPtr handle)
{
    var appID = "MyAppNoPin";

    var propGuid = new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}");
    var ID = new PropertyKey(propGuid, 5);              // System.AppUserModel.ID
    var PreventPinning = new PropertyKey(propGuid, 9);  // System.AppUserModel.PreventPinning

    //Important: Set PreventPinning before ID
    WindowProperties.SetWindowProperty(handle, PreventPinning, "True");
    WindowProperties.SetWindowProperty(handle, ID, appID);
}

【讨论】:

    【解决方案2】:

    我没有成功在最新的 Windows 10 更新上设置 RelaunchCommand 和 RelaunchDisplayNameResource。但我找到了一个解决方案,使用您的第一种方法将启动器固定到任务栏,设置一个通用 AppID:Pinning to the taskbar a "chained process"

    【讨论】:

      猜你喜欢
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多