- MainWindow.xaml:移除「注册启动协议 / 取消启动协议」按钮,仅保留状态文字 (「已注册 yuncang://(网页可叫醒本软件)」/ 未注册时提示重新运行安装包)。 - MainWindow.xaml.cs:删除 Protocol_Click(含确认框与 Unregister 调用), UpdateProtocolState 只更新文案(仍会提示"已注册但指向旧路径,重启会自动校正")。 - ProtocolHelper:注释更新——注册由 MSI 安装包完成、卸载时自动删除, 启动时由 App.EnsureProtocolRegistered 校正;Unregister 仅供卸载工具/排障。 理由:UI 上留「取消」按钮容易误点,点了网页就再也叫不醒软件,恢复还得重装。 验证:打包 1.0.4 安装后,界面按钮只剩「停止服务 / 复制唤醒地址 / 查询…」, 状态文字正常;版本 1.0.4 与后端 version.json 一致,启动不提示更新。
104 lines
4.3 KiB
C#
104 lines
4.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Microsoft.Win32;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>
|
||
/// 自定义协议(URL Scheme)——「网页叫醒本机应用」的唯一合法方式
|
||
///
|
||
/// 网页里写 <c>yuncang://open?code=123</c>,浏览器问一句「要打开云仓视频溯源软件吗?」,
|
||
/// 系统就会按注册表 <c>HKCU\Software\Classes\yuncang\shell\open\command</c> 找到本 exe 并拉起,
|
||
/// 形如:<c>"C:\...\YuncangTrace.LocalService.exe" "yuncang://open?code=123"</c>
|
||
///
|
||
/// 注意:
|
||
/// - 网页**不能**直接执行 exe,也不能拿到返回值(所以还要配合 127.0.0.1 上的 HTTP 服务);
|
||
/// - 协议注册在 HKCU,**不需要管理员权限**;
|
||
/// - **注册由安装包(MSI)完成**,卸载时 MSI 会自动删除;运行时启动也会校正到当前 exe
|
||
/// (<see cref="LocalHttpServer"/> 之外的处理见 App.EnsureProtocolRegistered)。
|
||
/// 这里的 <see cref="Unregister"/> 仅供卸载工具/排障使用,UI 上不再提供手动取消。
|
||
/// </summary>
|
||
public static class ProtocolHelper
|
||
{
|
||
/// <summary>协议名(前端用 VITE_APP_PROTOCOL 对齐;改了这里要一起改)</summary>
|
||
public const string Scheme = "yuncang";
|
||
|
||
/// <summary>资源管理器 / 浏览器的提示名</summary>
|
||
public const string DisplayName = "云仓视频溯源软件";
|
||
|
||
/// <summary>拼出唤醒地址:<c>yuncang://open?code=xxx</c></summary>
|
||
public static string LaunchUrl(string? code)
|
||
{
|
||
return string.IsNullOrWhiteSpace(code)
|
||
? $"{Scheme}://open"
|
||
: $"{Scheme}://open?code={Uri.EscapeDataString(code)}";
|
||
}
|
||
|
||
/// <summary>从命令行参数里找出协议地址(被协议拉起时 Windows 会把它作为第一个参数)</summary>
|
||
public static string? FirstLaunchUrl(IEnumerable<string> args)
|
||
{
|
||
return args.FirstOrDefault(arg =>
|
||
arg.StartsWith($"{Scheme}://", StringComparison.OrdinalIgnoreCase));
|
||
}
|
||
|
||
/// <summary>从 <c>yuncang://open?code=xxx</c> 里取出 code(没有就返回 null)</summary>
|
||
public static string? ParseCode(string? url)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(url)) return null;
|
||
|
||
var index = url.IndexOf('?');
|
||
if (index < 0 || index == url.Length - 1) return null;
|
||
|
||
foreach (var pair in url[(index + 1)..].Split('&', StringSplitOptions.RemoveEmptyEntries))
|
||
{
|
||
var parts = pair.Split('=', 2);
|
||
if (parts.Length == 2 && parts[0].Equals("code", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
var value = Uri.UnescapeDataString(parts[1].Replace('+', ' ')).Trim();
|
||
return value.Length == 0 ? null : value;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>协议是否已注册</summary>
|
||
public static bool IsRegistered()
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey($@"Software\Classes\{Scheme}\shell\open\command");
|
||
return key?.GetValue(null) is string value && value.Length > 0;
|
||
}
|
||
|
||
/// <summary>当前注册的命令行(界面上展示,方便排查指向了哪个 exe)</summary>
|
||
public static string? RegisteredCommand()
|
||
{
|
||
using var key = Registry.CurrentUser.OpenSubKey($@"Software\Classes\{Scheme}\shell\open\command");
|
||
return key?.GetValue(null) as string;
|
||
}
|
||
|
||
/// <summary>注册协议(指向当前 exe;HKCU,免管理员)</summary>
|
||
public static void Register()
|
||
{
|
||
var exePath = Environment.ProcessPath
|
||
?? throw new InvalidOperationException("拿不到当前程序的路径,无法注册启动协议");
|
||
|
||
using (var scheme = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{Scheme}"))
|
||
{
|
||
scheme.SetValue(null, $"URL:{DisplayName}");
|
||
scheme.SetValue("URL Protocol", string.Empty);
|
||
}
|
||
|
||
using (var command = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{Scheme}\shell\open\command"))
|
||
{
|
||
command.SetValue(null, $"\"{exePath}\" \"%1\"");
|
||
}
|
||
}
|
||
|
||
/// <summary>取消注册(卸载时调用)</summary>
|
||
public static void Unregister()
|
||
{
|
||
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{Scheme}", throwOnMissingSubKey: false);
|
||
}
|
||
}
|