Compare commits

...

1 Commits

Author SHA1 Message Date
zsyg
efd2c615bc 修改主要程序 2025-06-14 10:05:11 +08:00
6 changed files with 200 additions and 8 deletions

View File

@@ -16,10 +16,13 @@
<ItemGroup> <ItemGroup>
<None Include="help.txt" /> <None Include="help.txt" />
<None Include="resource\aria2c.exe"> <None Include="resource\*.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Include="img\png\WindowsCleaner.png"> <None Include="img\png\*.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="img\jpg\*.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Include="img\ico\icon.ico"> <None Include="img\ico\icon.ico">

View File

@@ -2,6 +2,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using AppStore;
namespace AppStore namespace AppStore
{ {
@@ -9,6 +10,7 @@ namespace AppStore
{ {
private Button btnApps = null!; private Button btnApps = null!;
private Button btnDownloads = null!; private Button btnDownloads = null!;
private Button btnSettings = null!;
private Panel contentPanel = null!; private Panel contentPanel = null!;
private void InitializeComponent() private void InitializeComponent()
@@ -32,7 +34,11 @@ namespace AppStore
btnApps.Size = new Size(100, 30); btnApps.Size = new Size(100, 30);
btnApps.Location = new Point(20, 10); btnApps.Location = new Point(20, 10);
btnApps.Font = new Font("Microsoft YaHei", 9); btnApps.Font = new Font("Microsoft YaHei", 9);
btnApps.Click += (s, e) => ShowAppsView(); btnApps.Click += (s, e) =>
{
Logger.Log("用户点击了'软件下载'按钮");
ShowAppsView();
};
buttonPanel.Controls.Add(btnApps); buttonPanel.Controls.Add(btnApps);
// 下载进度按钮 // 下载进度按钮
@@ -41,21 +47,46 @@ namespace AppStore
btnDownloads.Size = new Size(100, 30); btnDownloads.Size = new Size(100, 30);
btnDownloads.Location = new Point(140, 10); btnDownloads.Location = new Point(140, 10);
btnDownloads.Font = new Font("Microsoft YaHei", 9); btnDownloads.Font = new Font("Microsoft YaHei", 9);
btnDownloads.Click += (s, e) => ShowDownloadsView(); btnDownloads.Click += (s, e) =>
{
Logger.Log("用户点击了'下载进度'按钮");
ShowDownloadsView();
};
buttonPanel.Controls.Add(btnDownloads); buttonPanel.Controls.Add(btnDownloads);
this.Controls.Add(buttonPanel);
// 设置按钮
btnSettings = new Button
{
Text = "设置",
Size = new Size(100, 30),
Location = new Point(260, 10),
Font = new Font("Microsoft YaHei", 9)
};
btnSettings.Click += (s, e) =>
{
Logger.Log("用户点击了'设置'按钮");
ShowSettingsView();
};
buttonPanel.Controls.Add(btnSettings);
// 内容区域 // 内容区域
contentPanel = new Panel(); contentPanel = new Panel();
contentPanel.Dock = DockStyle.Fill; contentPanel.Dock = DockStyle.Fill;
contentPanel.Padding = new Padding(10); // 减少内边距 contentPanel.Padding = new Padding(10);
this.Controls.Add(contentPanel); this.Controls.Add(contentPanel);
this.Controls.Add(buttonPanel);
// 默认显示软件下载视图 // 默认显示软件下载视图
ShowAppsView(); ShowAppsView();
} }
private void ShowSettingsView()
{
var settingsForm = new SettingsForm();
settingsForm.ShowDialog();
}
private AppCard CreateAppCard(string appName, string downloadUrl, string iconPath) private AppCard CreateAppCard(string appName, string downloadUrl, string iconPath)
{ {
AppCard card = new AppCard(); AppCard card = new AppCard();
@@ -65,10 +96,12 @@ namespace AppStore
try try
{ {
card.AppIcon = Image.FromFile(iconPath); card.AppIcon = Image.FromFile(iconPath);
Logger.Log($"成功创建应用卡片: {appName}, 图标路径: {iconPath}");
} }
catch catch (Exception ex)
{ {
card.AppIcon = SystemIcons.Application.ToBitmap(); card.AppIcon = SystemIcons.Application.ToBitmap();
Logger.LogError($"创建应用卡片时加载图标失败: {appName}, 使用默认图标", ex);
} }
card.UpdateDisplay(); card.UpdateDisplay();
@@ -393,6 +426,7 @@ namespace AppStore
public MainForm() public MainForm()
{ {
Logger.Log("应用程序启动");
InitializeComponent(); InitializeComponent();
// 订阅下载管理器事件 // 订阅下载管理器事件
DownloadManager.Instance.DownloadAdded += OnDownloadAdded; DownloadManager.Instance.DownloadAdded += OnDownloadAdded;
@@ -429,6 +463,7 @@ namespace AppStore
return; return;
} }
Logger.Log($"添加新下载任务: {item.FileName}");
downloadItems.Add(item); downloadItems.Add(item);
downloadsFlowPanel?.Controls.Add(item); downloadsFlowPanel?.Controls.Add(item);
} }
@@ -441,6 +476,7 @@ namespace AppStore
return; return;
} }
Logger.Log($"下载进度更新: {item.FileName}, 进度: {item.Progress}%");
item.UpdateDisplay(); item.UpdateDisplay();
} }
@@ -452,6 +488,7 @@ namespace AppStore
return; return;
} }
Logger.Log($"下载完成: {item.FileName}, 状态: {item.Status}");
item.UpdateDisplay(); item.UpdateDisplay();
} }
} }

50
SettingsForm.cs Normal file
View File

@@ -0,0 +1,50 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace AppStore
{
public class SettingsForm : Form
{
private Button btnCleanLogs;
public SettingsForm()
{
this.Text = "设置";
this.Size = new Size(400, 300);
this.StartPosition = FormStartPosition.CenterParent;
btnCleanLogs = new Button();
btnCleanLogs.Text = "清理日志";
btnCleanLogs.Size = new Size(150, 40);
btnCleanLogs.Location = new Point(120, 100);
btnCleanLogs.Font = new Font("Microsoft YaHei", 10);
btnCleanLogs.Click += (s, e) => CleanLogs();
this.Controls.Add(btnCleanLogs);
}
private void CleanLogs()
{
try
{
string logCleanerPath = Path.Combine("resource", "log_cleaner.exe");
if (File.Exists(logCleanerPath))
{
Process.Start(logCleanerPath);
MessageBox.Show("日志清理程序已启动", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("日志清理程序未找到", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
Logger.LogError("清理日志时出错", ex);
MessageBox.Show($"清理日志时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

49
log_cleaner.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include <iostream>
#include <filesystem>
#include <chrono>
namespace fs = std::filesystem;
int main() {
try {
auto start = std::chrono::high_resolution_clock::now();
// 定义日志目录路径
fs::path logDir = "logs";
size_t deletedCount = 0;
size_t errorCount = 0;
// 检查目录是否存在
if (fs::exists(logDir) && fs::is_directory(logDir)) {
// 遍历并删除所有日志文件
for (const auto& entry : fs::directory_iterator(logDir)) {
try {
if (fs::is_regular_file(entry)) {
fs::remove(entry);
deletedCount++;
}
} catch (const std::exception& e) {
std::cerr << "删除文件失败: " << entry.path() << " - " << e.what() << std::endl;
errorCount++;
}
}
} else {
std::cout << "日志目录不存在,无需清理" << std::endl;
return 0;
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "日志清理完成: " << std::endl;
std::cout << "删除文件数: " << deletedCount << std::endl;
std::cout << "错误数: " << errorCount << std::endl;
std::cout << "耗时: " << duration.count() << " 毫秒" << std::endl;
} catch (const std::exception& e) {
std::cerr << "发生错误: " << e.what() << std::endl;
return 1;
}
return 0;
}

53
logger.cs Normal file
View File

@@ -0,0 +1,53 @@
using System;
using System.IO;
using System.Text;
namespace AppStore
{
public static class Logger
{
private static readonly string LogsDirectory = "logs";
private static readonly object LockObject = new object();
static Logger()
{
// 确保logs目录存在
if (!Directory.Exists(LogsDirectory))
{
Directory.CreateDirectory(LogsDirectory);
}
}
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);
}
}
}

BIN
resource/log_cleaner.exe Normal file

Binary file not shown.