修复「复制谷歌插件地址」剪贴板占用报错
- Clipboard.SetText 在别的进程持有剪贴板时会直接抛 CLIPBRD_E_CANT_OPEN(0x800401D0), 改为先 WPF 再 WinForms 两套实现各自重试(3 次 ×150ms) - 仍失败时提示原因(输入法 / 剪贴板工具 / 远程桌面 rdpclip),并可选择打开记事本显示路径 - 桌面端 README 补充该行为说明 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
228723e0c3
commit
27482723ff
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>剪贴板不可用时的兜底:把路径写进临时 txt 并用记事本打开</summary>
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写剪贴板:先 WPF 再 WinForms,各自重试几次。
|
||||
/// 单次 Clipboard.SetText 在别的进程正持有剪贴板时会直接抛 CLIPBRD_E_CANT_OPEN(0x800401D0)。
|
||||
/// </summary>
|
||||
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,
|
||||
|
||||
@ -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 组件一起删除;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user