添加注册uri的功能

This commit is contained in:
PANDA-JSR
2026-02-04 11:09:49 +08:00
parent a4868215f2
commit 8f6383cbe6
11 changed files with 363 additions and 3 deletions
@@ -26,6 +26,7 @@ namespace Ink_Canvas.Helpers
private const string IpcFilePrefix = "InkCanvasFileAssociation_";
private const string IpcBoardModePrefix = "InkCanvasBoardMode_";
private const string IpcShowModePrefix = "InkCanvasShowMode_";
private const string IpcUriCommandPrefix = "InkCanvasUriCommand_";
private const int IpcTimeout = 5000; // 5秒超时
/// <summary>
@@ -361,6 +362,57 @@ namespace Ink_Canvas.Helpers
}
}
/// <summary>
/// 尝试通过IPC将URI命令发送给已运行的实例
/// </summary>
/// <param name="uri">URI命令</param>
/// <returns>是否成功发送</returns>
public static bool TrySendUriCommandToExistingInstance(string uri)
{
try
{
LogHelper.WriteLogToFile($"尝试通过IPC发送URI命令给已运行实例: {uri}", LogHelper.LogType.Event);
// 创建IPC文件
string tempDir = Path.GetTempPath();
string ipcFileName = IpcUriCommandPrefix + Guid.NewGuid().ToString("N") + ".tmp";
string ipcFilePath = Path.Combine(tempDir, ipcFileName);
// 写入URI命令到IPC文件
File.WriteAllText(ipcFilePath, uri, Encoding.UTF8);
// 创建事件通知已运行实例
using (EventWaitHandle ipcEvent = new EventWaitHandle(false, EventResetMode.ManualReset, IpcEventName))
{
ipcEvent.Set();
}
// 等待一段时间让已运行实例处理命令
Thread.Sleep(1000);
// 清理IPC文件
try
{
if (File.Exists(ipcFilePath))
{
File.Delete(ipcFilePath);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"清理IPC文件失败: {ex.Message}", LogHelper.LogType.Warning);
}
LogHelper.WriteLogToFile("IPC URI命令发送完成", LogHelper.LogType.Event);
return true;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"通过IPC发送URI命令失败: {ex.Message}", LogHelper.LogType.Error);
return false;
}
}
/// <summary>
/// 启动IPC监听器,等待其他实例发送文件路径
/// </summary>
@@ -576,6 +628,56 @@ namespace Ink_Canvas.Helpers
catch { }
}
}
// 处理URI命令IPC文件
string[] uriCommandFiles = Directory.GetFiles(tempDir, IpcUriCommandPrefix + "*.tmp");
foreach (string ipcFile in uriCommandFiles)
{
try
{
// 读取命令内容
string uri = File.ReadAllText(ipcFile, Encoding.UTF8);
if (!string.IsNullOrEmpty(uri))
{
LogHelper.WriteLogToFile($"IPC接收到URI命令: {uri}", LogHelper.LogType.Event);
// 在UI线程中处理URI命令
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
try
{
// 获取主窗口并处理URI命令
if (Application.Current.MainWindow is MainWindow mainWindow)
{
mainWindow.HandleUriCommand(uri);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"IPC处理URI命令失败: {ex.Message}", LogHelper.LogType.Error);
}
}));
}
// 删除IPC文件
File.Delete(ipcFile);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"处理URI命令IPC文件失败: {ex.Message}", LogHelper.LogType.Warning);
// 尝试删除损坏的IPC文件
try
{
if (File.Exists(ipcFile))
{
File.Delete(ipcFile);
}
}
catch { }
}
}
}
catch (Exception ex)
{
+89
View File
@@ -0,0 +1,89 @@
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Security;
namespace Ink_Canvas.Helpers
{
public static class UriSchemeHelper
{
private const string SchemeName = "icc";
private const string FriendlyName = "URL:Ink Canvas Protocol";
public static bool RegisterUriScheme()
{
try
{
string exePath = Process.GetCurrentProcess().MainModule.FileName;
// 使用 CurrentUser\Software\Classes 代替 ClassesRoot,无需管理员权限
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + SchemeName))
{
key.SetValue("", FriendlyName);
key.SetValue("URL Protocol", "");
using (RegistryKey defaultIconKey = key.CreateSubKey("DefaultIcon"))
{
// 修正引号转义
defaultIconKey.SetValue("", "\"" + exePath + "\",1");
}
using (RegistryKey shellKey = key.CreateSubKey("shell"))
using (RegistryKey openKey = shellKey.CreateSubKey("open"))
using (RegistryKey commandKey = openKey.CreateSubKey("command"))
{
// 修正引号转义
commandKey.SetValue("", "\"" + exePath + "\" \"%1\"");
}
}
LogHelper.WriteLogToFile($"成功注册URI Scheme: {SchemeName}://", LogHelper.LogType.Event);
return true;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"注册URI Scheme失败: {ex.Message}", LogHelper.LogType.Error);
return false;
}
}
public static bool UnregisterUriScheme()
{
try
{
// 使用 CurrentUser\Software\Classes
Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\" + SchemeName, false);
LogHelper.WriteLogToFile($"成功注销URI Scheme: {SchemeName}://", LogHelper.LogType.Event);
return true;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"注销URI Scheme失败: {ex.Message}", LogHelper.LogType.Error);
return false;
}
}
public static bool IsUriSchemeRegistered()
{
try
{
// 使用 CurrentUser\Software\Classes
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes\" + SchemeName))
{
if (key == null) return false;
// 修正反斜杠路径
using (RegistryKey shellKey = key.OpenSubKey(@"shell\open\command"))
{
if (shellKey == null) return false;
string command = shellKey.GetValue("") as string;
string exePath = Process.GetCurrentProcess().MainModule.FileName;
return !string.IsNullOrEmpty(command) && command.Contains(exePath);
}
}
}
catch
{
return false;
}
}
}
}