diff --git a/desktop/MainWindow.xaml.cs b/desktop/MainWindow.xaml.cs
index 4825622..1593a07 100644
--- a/desktop/MainWindow.xaml.cs
+++ b/desktop/MainWindow.xaml.cs
@@ -781,30 +781,97 @@ public partial class MainWindow : Window
private void CopyExtensionPath_Click(object sender, RoutedEventArgs e)
{
var path = Path.Combine(AppContext.BaseDirectory, "extension");
- try
+ if (!Directory.Exists(path))
{
- if (!Directory.Exists(path))
- {
- MessageBox.Show(this,
- $"没有找到谷歌插件目录:\n\n{path}\n\n请安装最新版本后再试。",
- "复制谷歌插件地址", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
+ MessageBox.Show(this,
+ $"没有找到谷歌插件目录:\n\n{path}\n\n请安装最新版本后再试。",
+ "复制谷歌插件地址", MessageBoxButton.OK, MessageBoxImage.Warning);
+ return;
+ }
- Clipboard.SetText(path);
+ if (TrySetClipboardText(path))
+ {
AppLog.Write($"已复制谷歌插件地址:{path}");
MessageBox.Show(this,
$"已复制插件路径:\n\n{path}\n\n打开 chrome://extensions → 打开「开发者模式」→「加载已解压的扩展程序」→ 粘贴该路径。",
"复制谷歌插件地址", MessageBoxButton.OK, MessageBoxImage.Information);
+ return;
+ }
+
+ AppLog.Write($"复制谷歌插件地址失败(剪贴板被其他程序占用):{path}");
+ var openNotepad = MessageBox.Show(this,
+ "剪贴板被其他程序占用(输入法 / 剪贴板工具 / 远程桌面都可能),复制失败。\n\n" +
+ "· 稍等一秒再点一次「复制谷歌插件地址」,通常就好;\n" +
+ "· 远程桌面里遇到的话,在远程机器上重启 rdpclip.exe 后重试;\n" +
+ $"· 也可以手动输入这个路径:\n{path}\n\n" +
+ "要打开记事本显示这个路径吗(方便对照抄写)?",
+ "复制谷歌插件地址", MessageBoxButton.YesNo, MessageBoxImage.Warning);
+
+ if (openNotepad == MessageBoxResult.Yes)
+ {
+ ShowPathInNotepad(path);
+ }
+ }
+
+ /// 剪贴板不可用时的兜底:把路径写进临时 txt 并用记事本打开
+ private static void ShowPathInNotepad(string path)
+ {
+ try
+ {
+ var file = Path.Combine(Path.GetTempPath(), "yuncang-extension-path.txt");
+ File.WriteAllText(file, path, Encoding.UTF8);
+ Process.Start(new ProcessStartInfo("notepad.exe", $"\"{file}\"") { UseShellExecute = true });
}
catch (Exception ex)
{
- AppLog.Write($"复制谷歌插件地址失败:{ex.Message}");
- MessageBox.Show(this, $"复制插件路径失败:{ex.Message}\n\n{path}",
- "复制谷歌插件地址", MessageBoxButton.OK, MessageBoxImage.Warning);
+ AppLog.Write($"打开记事本显示插件路径失败:{ex.Message}");
}
}
+ ///
+ /// 写剪贴板:先 WPF 再 WinForms,各自重试几次。
+ /// 单次 Clipboard.SetText 在别的进程正持有剪贴板时会直接抛 CLIPBRD_E_CANT_OPEN(0x800401D0)。
+ ///
+ private static bool TrySetClipboardText(string text)
+ {
+ if (RetryClipboardWrite(() => Clipboard.SetDataObject(text, true), "WPF"))
+ {
+ return true;
+ }
+
+ if (RetryClipboardWrite(() => System.Windows.Forms.Clipboard.SetDataObject(text, true), "WinForms"))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ private static bool RetryClipboardWrite(Action write, string api)
+ {
+ const int attempts = 3;
+ const int delayMs = 150;
+
+ for (var attempt = 1; attempt <= attempts; attempt++)
+ {
+ try
+ {
+ write();
+ return true;
+ }
+ catch (Exception ex)
+ {
+ AppLog.Write($"{api} 写剪贴板失败(第 {attempt}/{attempts} 次):{ex.Message}");
+ if (attempt < attempts)
+ {
+ Thread.Sleep(delayMs);
+ }
+ }
+ }
+
+ return false;
+ }
+
private void ClearLogs_Click(object sender, RoutedEventArgs e)
{
var answer = MessageBox.Show(this,
diff --git a/desktop/README.md b/desktop/README.md
index 4a80ec6..c9b4f16 100644
--- a/desktop/README.md
+++ b/desktop/README.md
@@ -230,6 +230,8 @@ cd desktop\installer
(perUser 安装即 `%LOCALAPPDATA%\Programs\YuncangTrace\extension`):
- 菜单「工具 → 复制谷歌插件地址」直接把该目录的 **Windows 路径**(如 `C:\Users\xxx\AppData\Local\Programs\YuncangTrace\extension`)复制到剪贴板;
+ 写剪贴板会重试(WPF → WinForms,各 3 次 ×150ms),被输入法/剪贴板工具/远程桌面占用时不会直接报 `CLIPBRD_E_CANT_OPEN`;
+ 仍然失败会提示原因,并可以选「打开记事本」把路径显示出来手动抄写;
- 用户打开 `chrome://extensions` → 打开「开发者模式」→「加载已解压的扩展程序」→ 粘贴路径即可;
- 卸载时该目录随 MSI 组件一起删除;