From 815ba41bdc04e7701dabdbf060c495208381cacb Mon Sep 17 00:00:00 2001 From: zsyg <3872006562@qq.com> Date: Wed, 2 Jul 2025 16:53:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E9=AB=98=E4=BB=A3=E7=A0=81=E8=B4=A8?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AppCard.cs | 183 ++++++++++++++++++++++++++++++--------------- DownloadManager.cs | 17 +++-- logger.cs | 148 +++++++++++++++++++----------------- 3 files changed, 214 insertions(+), 134 deletions(-) diff --git a/AppCard.cs b/AppCard.cs index c792fd8..4fc6789 100644 --- a/AppCard.cs +++ b/AppCard.cs @@ -59,28 +59,53 @@ namespace AppStore InitializeBorder(); }); - // 应用图标 - iconBox = new PictureBox(); - iconBox.Size = new Size(80, 80); - iconBox.Location = new Point((Width - 80) / 2, 15); - iconBox.SizeMode = PictureBoxSizeMode.StretchImage; - this.Controls.Add(iconBox); + // 应用图标 - 添加null检查 + if (iconBox != null && this != null && this.Controls != null) + { + iconBox.Size = new Size(80, 80); + iconBox.Location = new Point((Width - 80) / 2, 15); + iconBox.SizeMode = PictureBoxSizeMode.StretchImage; + this.Controls.Add(iconBox); + } + else + { + Logger.LogWarning("iconBox或Controls为null"); + } // 应用名称 - 使用Panel包裹Label实现边框颜色 - // namePanel已在构造函数中初始化 - namePanel.Size = new Size(Width - 20, 40); - namePanel.Location = new Point(10, 100); - namePanel.Paint += (sender, e) => { - ControlPaint.DrawBorder(e.Graphics, namePanel.ClientRectangle, - borderColor, ButtonBorderStyle.Solid); - }; + if (namePanel != null) + { + namePanel.Size = new Size(Width - 20, 40); + namePanel.Location = new Point(10, 100); + namePanel.Paint += (sender, e) => { + try + { + if (e != null && e.Graphics != null && namePanel != null) + { + var rect = namePanel.ClientRectangle; + if (rect.Width > 0 && rect.Height > 0) + { + ControlPaint.DrawBorder(e.Graphics, rect, + borderColor, ButtonBorderStyle.Solid); + } + } + } + catch (Exception ex) + { + Logger.LogWarning($"绘制namePanel边框失败: {ex.Message}"); + } + }; + } nameLabel = new Label(); nameLabel.Dock = DockStyle.Fill; nameLabel.Font = new Font("Microsoft YaHei", 10, FontStyle.Bold); nameLabel.TextAlign = ContentAlignment.MiddleCenter; - namePanel.Controls.Add(nameLabel); + if (namePanel != null && nameLabel != null) + { + namePanel.Controls.Add(nameLabel); + } // 初始主题设置 UpdateLabelTheme(); @@ -88,32 +113,43 @@ namespace AppStore // 订阅主题变化事件 ThemeManager.ThemeChanged += (theme) => UpdateLabelTheme(); - this.Controls.Add(namePanel); + if (this != null && this.Controls != null && namePanel != null) + { + this.Controls.Add(namePanel); + } - // 下载按钮 - downloadBtn = new Button(); - downloadBtn.Text = "下载"; - downloadBtn.Size = new Size(100, 32); - downloadBtn.Location = new Point((Width - 100) / 2, 150); - downloadBtn.BackColor = Color.FromArgb(0, 120, 215); - downloadBtn.ForeColor = Color.White; - downloadBtn.FlatStyle = FlatStyle.Flat; - downloadBtn.FlatAppearance.BorderSize = 0; - downloadBtn.Cursor = Cursors.Hand; - downloadBtn.Font = new Font("Microsoft YaHei", 9); - - // 按钮悬停效果 - downloadBtn.MouseEnter += (s, e) => { - downloadBtn.BackColor = Color.FromArgb(0, 150, 255); - }; - - downloadBtn.MouseLeave += (s, e) => { + // 下载按钮 - 添加null检查 + if (downloadBtn != null) + { + downloadBtn.Text = "下载"; + downloadBtn.Size = new Size(100, 32); + downloadBtn.Location = new Point((Width - 100) / 2, 150); downloadBtn.BackColor = Color.FromArgb(0, 120, 215); - }; - - downloadBtn.Click += DownloadBtn_Click; - this.Controls.Add(downloadBtn); - downloadBtn.Visible = ShowDownloadButton; + downloadBtn.ForeColor = Color.White; + downloadBtn.FlatStyle = FlatStyle.Flat; + downloadBtn.FlatAppearance.BorderSize = 0; + downloadBtn.Cursor = Cursors.Hand; + downloadBtn.Font = new Font("Microsoft YaHei", 9); + + // 按钮悬停效果 - 添加null检查 + downloadBtn.MouseEnter += (s, e) => { + if (downloadBtn != null) + { + downloadBtn.BackColor = Color.FromArgb(0, 150, 255); + } + }; + + downloadBtn.MouseLeave += (s, e) => { + if (downloadBtn != null) + { + downloadBtn.BackColor = Color.FromArgb(0, 120, 215); + } + }; + + downloadBtn.Click += DownloadBtn_Click; + this.Controls.Add(downloadBtn); + downloadBtn.Visible = ShowDownloadButton; + } } private void UpdateLabelTheme() @@ -132,7 +168,14 @@ namespace AppStore namePanel.BackColor = Color.White; borderColor = SystemColors.ControlDark; } - namePanel.Invalidate(); // 触发重绘 + if (namePanel != null && !namePanel.IsDisposed) + { + namePanel.Invalidate(); // 触发重绘 + } + else + { + Logger.LogWarning("namePanel为null或已释放"); + } } /// @@ -262,22 +305,37 @@ namespace AppStore }; // 启动C++程序计算路径 - using (var process = Process.Start(startInfo)) { - process.WaitForExit(); - - // 检查计算结果 - if (process.ExitCode == 0 && File.Exists(tempFile)) { - // 读取生成的路径点 - var lines = File.ReadAllLines(tempFile); - PointF[] points = lines.Select(line => { - var parts = line.Split(','); // 解析坐标 - return new PointF(float.Parse(parts[0]), float.Parse(parts[1])); - }).ToArray(); - - // 创建并缓存路径对象 - path = new System.Drawing.Drawing2D.GraphicsPath(); - path.AddLines(points); - PathCache.TryAdd(cacheKey, path); + if (startInfo != null) + { + using (var process = Process.Start(startInfo)) + { + if (process != null) + { + process.WaitForExit(); + + // 检查计算结果 + if (process.ExitCode == 0 && File.Exists(tempFile)) + { + try + { + // 读取生成的路径点 + var lines = File.ReadAllLines(tempFile); + PointF[] points = lines.Select(line => { + var parts = line.Split(','); // 解析坐标 + return new PointF(float.Parse(parts[0]), float.Parse(parts[1])); + }).ToArray(); + + // 创建并缓存路径对象 + path = new System.Drawing.Drawing2D.GraphicsPath(); + path.AddLines(points); + PathCache.TryAdd(cacheKey, path); + } + catch (Exception ex) + { + Logger.LogWarning($"读取路径点失败: {ex.Message}"); + } + } + } } } } catch { @@ -353,10 +411,14 @@ namespace AppStore public void UpdateDisplay() { - if (nameLabel != null) + if (nameLabel != null && AppName != null) { nameLabel.Text = AppName; } + else + { + Logger.LogWarning("nameLabel或AppName为null"); + } if (iconBox != null && AppIcon != null) { iconBox.Image = AppIcon; @@ -370,14 +432,15 @@ namespace AppStore // 更严格的null检查 // 更严格的null检查,包括DownloadManager.Instance和其方法 // 全面的null和状态检查 + var downloadManager = DownloadManager.Instance; if (sender == null || e == null || string.IsNullOrWhiteSpace(DownloadUrl) || string.IsNullOrWhiteSpace(AppName) || !this.IsHandleCreated || this.IsDisposed || - DownloadManager.Instance == null || - DownloadManager.Instance.DownloadItems == null || - DownloadManager.Instance.StartDownload == null) + downloadManager == null || + downloadManager.DownloadItems == null || + downloadManager.StartDownload == null) { return; } @@ -385,7 +448,7 @@ namespace AppStore string safeAppName = AppName ?? "未知应用"; string fileName = $"{safeAppName.Replace(" ", "_")}.exe"; - DownloadManager.Instance.StartDownload(fileName, DownloadUrl); + downloadManager.StartDownload(fileName, DownloadUrl); string message = $"已开始下载: {safeAppName}"; this.Invoke((MethodInvoker)delegate { diff --git a/DownloadManager.cs b/DownloadManager.cs index a6a8cc5..e14e9bd 100644 --- a/DownloadManager.cs +++ b/DownloadManager.cs @@ -455,12 +455,16 @@ namespace AppStore var downloadsFolderGuid = new Guid("374DE290-123F-4565-9164-39C4925E467B"); if (SHGetKnownFolderPath(downloadsFolderGuid, 0, IntPtr.Zero, out pathPtr) == 0) { - string defaultPath = Marshal.PtrToStringUni(pathPtr); + string? defaultPath = Marshal.PtrToStringUni(pathPtr); if (!string.IsNullOrEmpty(defaultPath)) { Directory.CreateDirectory(defaultPath); return defaultPath; } + else + { + Logger.LogWarning("获取到的系统下载路径为空"); + } } } catch (Exception ex) @@ -477,16 +481,19 @@ namespace AppStore // 3. 最终回退到相对路径 ~/Downloads string relativePath = "~/Downloads"; - string fallbackPath = relativePath.Replace("~", - Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)); + string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) ?? string.Empty; + string fallbackPath = relativePath.Replace("~", userProfile); fallbackPath = Path.GetFullPath(fallbackPath); try { Directory.CreateDirectory(fallbackPath); // 测试路径可写性 string testFile = Path.Combine(fallbackPath, "write_test.tmp"); - File.WriteAllText(testFile, "test"); - File.Delete(testFile); + if (!string.IsNullOrEmpty(testFile)) + { + File.WriteAllText(testFile, "test"); + File.Delete(testFile); + } return fallbackPath; } catch { diff --git a/logger.cs b/logger.cs index 74c914c..9ac2492 100644 --- a/logger.cs +++ b/logger.cs @@ -1,69 +1,79 @@ - // _ _ - //| | _____ _ __| |_ __ _ _ __ _ __ ____ - //| |/ / _ \| '__| __/ _` | '_ \| '_ \ ____|_ / - //| | (_) | | | || (_| | |_) | |_) |_____/ / - //|_|\_\___/|_| \__\__,_| .__/| .__/ /___| - // |_| |_| -using System; -using System.IO; -using System.Text; - -namespace AppStore -{ - public static class Logger - { - private static readonly string LogsDirectory = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - "zsyg", "kortapp-z", ".logs"); - private static readonly object LockObject = new object(); - - static Logger() - { - try - { - // 确保logs目录存在 - if (!Directory.Exists(LogsDirectory)) - { - Directory.CreateDirectory(LogsDirectory); - } - } - catch (Exception ex) - { - Console.WriteLine($"无法创建日志目录: {LogsDirectory}, 错误: {ex.Message}"); - throw; - } - } - - public static void Log(string message) - { - lock (LockObject) - { - try - { - string fileName = $"{DateTime.Now:yyyyMMddHHmmss}.log"; - string filePath = Path.Combine(LogsDirectory, fileName); - - using (StreamWriter writer = new StreamWriter(filePath, true, Encoding.UTF8)) - { - writer.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}"); - } - } - catch (Exception ex) - { - // 日志记录失败时输出到控制台 - Console.WriteLine($"日志记录失败: {ex.Message}"); - } - } - } - - public static void LogError(string message, Exception? ex = null) - { - string errorMessage = $"ERROR: {message}"; - if (ex != null) - { - errorMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}"; - } - Log(errorMessage); - } - } -} + // _ _ + //| | _____ _ __| |_ __ _ _ __ _ __ ____ + //| |/ / _ \| '__| __/ _` | '_ \| '_ \ ____|_ / + //| | (_) | | | || (_| | |_) | |_) |_____/ / + //|_|\_\___/|_| \__\__,_| .__/| .__/ /___| + // |_| |_| +using System; +using System.IO; +using System.Text; + +namespace AppStore +{ + public static class Logger + { + private static readonly string LogsDirectory = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "zsyg", "kortapp-z", ".logs"); + private static readonly object LockObject = new object(); + + static Logger() + { + try + { + // 确保logs目录存在 + if (!Directory.Exists(LogsDirectory)) + { + Directory.CreateDirectory(LogsDirectory); + } + } + catch (Exception ex) + { + Console.WriteLine($"无法创建日志目录: {LogsDirectory}, 错误: {ex.Message}"); + throw; + } + } + + public static void Log(string message) + { + lock (LockObject) + { + try + { + string fileName = $"{DateTime.Now:yyyyMMddHHmmss}.log"; + string filePath = Path.Combine(LogsDirectory, fileName); + + using (StreamWriter writer = new StreamWriter(filePath, true, Encoding.UTF8)) + { + writer.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}"); + } + } + catch (Exception ex) + { + // 日志记录失败时输出到控制台 + Console.WriteLine($"日志记录失败: {ex.Message}"); + } + } + } + + public static void LogError(string message, Exception? ex = null) + { + string errorMessage = $"ERROR: {message}"; + if (ex != null) + { + errorMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}"; + } + Log(errorMessage); + } + + public static void LogWarning(string message, Exception? ex = null) + { + string warningMessage = $"WARNING: {message}"; + if (ex != null) + { + warningMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}"; + } + Log(warningMessage); + } + } +}