Skip to content
4 changes: 2 additions & 2 deletions Ink Canvas/Helpers/ForegroundWindowInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public static string ProcessPath()
Process process = Process.GetProcessById((int)processId);
return process.MainModule.FileName;
}
catch
catch (Exception ex)
{
// Process with the given ID not found
Debug.WriteLine($"[ForegroundWindowInfo] 获取前台进程路径失败: {ex.Message}");
return "Unknown";
}
}
Expand Down
8 changes: 4 additions & 4 deletions Ink Canvas/Helpers/FullScreenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public static void MarkFullscreenWindowTaskbarList(IntPtr hwnd, bool isFullscree
var obj = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_TaskbarList));
(obj as ITaskbarList2)?.MarkFullscreenWindow(hwnd, isFullscreen);
}
catch
catch (Exception ex)
{
//应该不会挂
System.Diagnostics.Debug.WriteLine($"[FullScreenHelper] 标记任务栏全屏状态失败: {ex.Message}");
}
}

Expand Down Expand Up @@ -286,9 +286,9 @@ private static IntPtr KeepFullScreenHook(IntPtr hwnd, int msg, IntPtr wParam, In
}
}
}
catch
catch (Exception ex)
{
// 这里也不需要日志啥的,只是为了防止上面有逗比逻辑,在消息循环里面炸了
System.Diagnostics.Debug.WriteLine($"[FullScreenHelper] 处理全屏 Hook 消息失败: {ex.Message}");
}
}

Expand Down
3 changes: 2 additions & 1 deletion Ink Canvas/Helpers/IACoreDllExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ private static bool IsValidDll(string filePath)

return false;
}
catch
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[IACoreDllExtractor] 校验DLL失败: {filePath}, {ex.Message}");
return false;
}
}
Expand Down
4 changes: 3 additions & 1 deletion Ink Canvas/Helpers/LocalizationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ink_Canvas.Properties;
using System;
using System.Globalization;
using System.Threading;

Expand Down Expand Up @@ -40,8 +41,9 @@ public static bool TrySetCulture(string cultureName)
CurrentCulture = culture;
return true;
}
catch
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[LocalizationHelper] 切换语言失败: {ex.Message}");
return false;
}
}
Expand Down
62 changes: 44 additions & 18 deletions Ink Canvas/Helpers/ProcessProtectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ internal static class ProcessProtectionManager
"AutoUpdate"
};

private static readonly string DebugTag = "[ProcessProtectionManager]";

private static void WriteDebugLog(string message, Exception ex = null)
{
if (ex == null)
{
System.Diagnostics.Debug.WriteLine($"{DebugTag} {message}");
return;
}

System.Diagnostics.Debug.WriteLine($"{DebugTag} {message}: {ex.Message}");
}


public static bool Enabled
{
get { lock (_lock) return _enabled; }
Expand Down Expand Up @@ -70,7 +84,10 @@ public static void SetEnabled(bool enabled)
{
LogHelper.WriteLogToFile($"ProcessProtectionManager.SetEnabled 后台执行失败: {ex.Message}", LogHelper.LogType.Warning);
}
catch { }
catch (Exception logEx)
{
WriteDebugLog("写入告警日志失败", logEx);
}
}
});
}
Expand Down Expand Up @@ -104,7 +121,7 @@ public static void WithWriteAccess(string targetPath, Action action)
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[ProcessProtectionManager] 写日志失败: {ex.Message}");
WriteDebugLog("写日志失败", ex);
}

var normPath = NormalizePath(targetPath);
Expand Down Expand Up @@ -139,14 +156,14 @@ public static void WithWriteAccess(string targetPath, Action action)
{
foreach (var kv in fallbackFiles)
{
try { kv.Value.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
try { kv.Value.Dispose(); } catch (Exception ex) { WriteDebugLog("释放文件锁失败", ex); }
}
}
if (fallbackDirs != null)
{
foreach (var kv in fallbackDirs)
{
try { kv.Value.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
try { kv.Value.Dispose(); } catch (Exception ex) { WriteDebugLog("释放文件锁失败", ex); }
}
}

Expand All @@ -161,7 +178,7 @@ public static void WithWriteAccess(string targetPath, Action action)
Enable(rescanRoot: false, rescanDirs: dirsChain);
}
}
catch { }
catch (Exception ex) { WriteDebugLog("降级路径恢复保护失败", ex); }
}
return;
}
Expand Down Expand Up @@ -199,14 +216,14 @@ public static void WithWriteAccess(string targetPath, Action action)
{
foreach (var kv in releasedFiles)
{
try { kv.Value.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
try { kv.Value.Dispose(); } catch (Exception ex) { WriteDebugLog("释放文件锁失败", ex); }
}
}
if (releasedDirs != null)
{
foreach (var kv in releasedDirs)
{
try { kv.Value.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
try { kv.Value.Dispose(); } catch (Exception ex) { WriteDebugLog("释放文件锁失败", ex); }
}
}

Expand All @@ -221,8 +238,9 @@ public static void WithWriteAccess(string targetPath, Action action)
Enable(rescanRoot: false, rescanDirs: dirsToToggle);
}
}
catch
catch (Exception ex)
{
WriteDebugLog("恢复目录/文件保护失败", ex);
}

Interlocked.Exchange(ref _writeGate, 0);
Expand Down Expand Up @@ -305,8 +323,9 @@ private static void Enable(bool rescanRoot, IEnumerable<string> rescanDirs)
}
}
}
catch
catch (Exception ex)
{
WriteDebugLog("启用保护失败", ex);
}
}

Expand All @@ -323,13 +342,13 @@ private static void Disable()
{
foreach (var kv in _lockedFiles)
{
try { kv.Value.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
try { kv.Value.Dispose(); } catch (Exception ex) { WriteDebugLog("释放文件锁失败", ex); }
}
_lockedFiles.Clear();

foreach (var kv in _lockedDirs)
{
try { kv.Value.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
try { kv.Value.Dispose(); } catch (Exception ex) { WriteDebugLog("释放文件锁失败", ex); }
}
_lockedDirs.Clear();
}
Expand All @@ -356,8 +375,9 @@ private static void LockDirectoryRecursive(string root)
}
}
}
catch
catch (Exception ex)
{
WriteDebugLog("遍历锁定对象时发生异常", ex);
}
}

Expand Down Expand Up @@ -388,8 +408,9 @@ private static void LockFilesRecursive(string root)
}
}
}
catch
catch (Exception ex)
{
WriteDebugLog("遍历锁定对象时发生异常", ex);
}
}

Expand All @@ -408,8 +429,9 @@ private static void LockFile(string filePath)
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
_lockedFiles[filePath] = fs;
}
catch
catch (Exception ex)
{
WriteDebugLog("锁定文件失败", ex);
}
}
}
Expand All @@ -432,8 +454,9 @@ private static void LockDirectory(string dirPath)
_lockedDirs[dirPath] = handle;
}
}
catch
catch (Exception ex)
{
WriteDebugLog("锁定目录失败", ex);
}
}
}
Expand All @@ -450,8 +473,9 @@ private static string NormalizePath(string p)
if (string.IsNullOrWhiteSpace(p)) return p;
return Path.GetFullPath(p.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
}
catch
catch (Exception ex)
{
WriteDebugLog("路径规范化失败", ex);
return p;
}
}
Expand All @@ -478,8 +502,9 @@ private static List<string> GetDirChainToRoot(string path)
dir = NormalizePath(Path.GetDirectoryName(dir));
}
}
catch
catch (Exception ex)
{
WriteDebugLog("构建目录链失败", ex);
}
return list;
}
Expand All @@ -505,8 +530,9 @@ private static bool IsExcludedPath(string path)
}
}
}
catch
catch (Exception ex)
{
WriteDebugLog("排除路径检查失败", ex);
}
return false;
}
Expand Down
15 changes: 10 additions & 5 deletions Ink Canvas/Helpers/ScreenDetectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ private static Rectangle GetWindowScreenBounds(Window window)
(int)(bottomRight.X - topLeft.X),
(int)(bottomRight.Y - topLeft.Y));
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"获取窗口屏幕边界失败: {ex.Message}", LogHelper.LogType.Warning);
// 如果无法获取精确位置,返回窗口的Left和Top
return new Rectangle(
(int)window.Left,
Expand All @@ -97,8 +98,9 @@ public static bool HasMultipleScreens()
{
return Screen.AllScreens.Length > 1;
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"检测多屏状态失败: {ex.Message}", LogHelper.LogType.Warning);
return false;
}
}
Expand All @@ -113,8 +115,9 @@ public static Screen GetPrimaryScreen()
{
return Screen.PrimaryScreen;
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"获取主屏幕失败: {ex.Message}", LogHelper.LogType.Warning);
return null;
}
}
Expand All @@ -129,8 +132,9 @@ public static Screen[] GetAllScreens()
{
return Screen.AllScreens;
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"获取屏幕列表失败: {ex.Message}", LogHelper.LogType.Warning);
return new Screen[] { Screen.PrimaryScreen };
}
}
Expand All @@ -147,8 +151,9 @@ public static bool IsWindowOnPrimaryScreen(Window window)
var windowScreen = GetWindowScreen(window);
return windowScreen == Screen.PrimaryScreen;
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"检测窗口是否在主屏幕失败: {ex.Message}", LogHelper.LogType.Warning);
return true; // 出错时假设在主屏幕
}
}
Expand Down
10 changes: 6 additions & 4 deletions Ink Canvas/Helpers/WebDavUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ public static async Task<bool> UploadFileAsync(string filePath, CancellationToke
{
throw;
}
catch (Exception)
catch (Exception ex)
{
LogHelper.WriteLogToFile($"WebDav 上传失败: {ex.Message}", LogHelper.LogType.Warning);
return false;
}
}
Expand Down Expand Up @@ -137,9 +138,9 @@ private static async Task EnsureDirectoryExistsAsync(IWebDavClient client, strin
await client.Mkcol(currentPath);
}
}
catch (Exception)
catch (Exception ex)
{
// 静默处理目录创建错误
LogHelper.WriteLogToFile($"WebDav 创建目录失败: {directoryPath}, {ex.Message}", LogHelper.LogType.Warning);
}
}

Expand All @@ -162,8 +163,9 @@ public static bool IsWebDavEnabled()
new Uri(webDavUrl);
return true;
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"WebDav 地址无效: {webDavUrl}, {ex.Message}", LogHelper.LogType.Warning);
return false;
}
}
Expand Down
6 changes: 4 additions & 2 deletions Ink Canvas/Helpers/WindowsNotificationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public static void ShowNewVersionToast(string version)
ShowToastForModernWindows(version);
}
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"显示更新通知失败: {ex.Message}", LogHelper.LogType.Warning);
}
}

Expand All @@ -45,8 +46,9 @@ private static void ShowBalloonForWin7(string version)
$"发现新版本!:{version}",
BalloonIcon.Info);
}
catch
catch (Exception ex)
{
LogHelper.WriteLogToFile($"显示 Win7 气泡通知失败: {ex.Message}", LogHelper.LogType.Warning);
}
});
}
Expand Down
Loading