QianTools 2.3.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package QianTools --version 2.3.3
                    
NuGet\Install-Package QianTools -Version 2.3.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="QianTools" Version="2.3.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QianTools" Version="2.3.3" />
                    
Directory.Packages.props
<PackageReference Include="QianTools" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add QianTools --version 2.3.3
                    
#r "nuget: QianTools, 2.3.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package QianTools@2.3.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=QianTools&version=2.3.3
                    
Install as a Cake Addin
#tool nuget:?package=QianTools&version=2.3.3
                    
Install as a Cake Tool

QianTools

.NET 工业自动化与系统管理工具库,支持 net6.0 / net8.0。

作者:钱军涛 | 公司:浪涛科技有限公司 | 许可证:MIT

安装

dotnet add package QianTools

项目结构

QianTools/
├── Core/                    # 基础设施(日志、配置)
│   ├── LogManager.cs        # 日志管理器(支持 ILogger 接口、级别过滤、文件滚动)
│   ├── ILogger.cs           # 日志接口
│   ├── LogLevel.cs          # 日志级别枚举
│   ├── ConfigManager.cs     # 配置管理器(JSON/XML,策略模式可扩展)
│   ├── IConfigParser.cs     # 配置解析器接口
│   ├── JsonParser.cs        # JSON 解析器
│   └── XmlParser.cs         # XML 解析器
├── Helpers/                 # 通用工具类
│   ├── AllTools.cs          # 统一外观入口(FileTools / CryptoTools / NetworkTools)
│   ├── CryptoHelper.cs      # 加密与哈希(SHA256、MD5)
│   ├── FileHelper.cs        # 文件/目录复制与删除
│   └── NetworkHelper.cs     # HTTP 文件下载
├── System/                  # 系统硬件信息
│   ├── SystemHardwareInfo.cs    # 串口、磁盘、CPU、操作系统、网络适配器
│   └── MachineCodeGenerator.cs # 机器码生成与授权验证
├── Plc/                     # PLC 通信
│   ├── MelsecPlcService.cs  # 三菱 FX 底层通信服务(ASCII 协议)
│   └── MelsecPlcHelper.cs   # 上层静态封装
├── Application/             # 应用层服务
│   ├── AutoStartManager.cs  # 注册表开机自启动
│   ├── ShortcutCreator.cs   # 桌面快捷方式
│   ├── VerifyInstallation.cs# 软件安装与验证
│   └── MesClient.cs         # MES HTTP 客户端
├── QianTools.csproj
└── README.md

功能模块

功能模块

Core — 基础设施

日志管理 — LogManager
// 使用静态方法(默认单例)
LogManager.Info("设备启动成功");
LogManager.Error($"通信失败: {ex.Message}");

// 自定义日志路径和文件滚动
LogManager.Initialize(@"D:\AppLogs\myapp.log");
LogManager.MinLogLevel = LogLevel.Debug;
LogManager.MaxFileSizeBytes = 10 * 1024 * 1024;  // 10MB 自动滚动

// 通过 ILogger 接口注入(支持单元测试)
ILogger logger = LogManager.Instance;
logger.LogInfo("消息");
配置管理 — ConfigManager

支持 JSON(默认)和 XML 两种格式,策略模式可扩展。

// ===== JSON 模式(默认) =====
var cfg = new ConfigManager();                          // 或 ConfigManager.Default
cfg.Set("AppSettings", new AppSettings { Theme = "Dark" });
var settings = cfg.Get<AppSettings>("AppSettings");     // 从内存读取
cfg.Save();                                              // 持久化到文件
cfg.Load();                                              // 从文件加载到内存
cfg.Remove("AppSettings");

// ===== XML 模式 =====
var xmlCfg = new ConfigManager(useXml: true, customPath: @"D:\MyApp\Config");
xmlCfg.Set("AppSettings", new AppSettings { Theme = "Dark" });
xmlCfg.Save();  // → D:\MyApp\Config\config.xml

// ===== 错误监听 =====
ConfigManager.OnError += msg => Console.WriteLine(msg);

Helpers — 通用工具

统一入口 — AllTools
// 通过 AllTools 静态子类访问
AllTools.FileTools.CopyDirectory(@"C:\Source", @"D:\Backup", overwrite: true);
string sha256 = AllTools.CryptoTools.ComputeSha256Hash("hello");
await AllTools.NetworkTools.DownloadFileAsync(url, destinationPath);
文件操作 / 加密 / 网络
// 文件
FileHelper.CopyDirectory(@"C:\Source", @"D:\Backup", overwrite: true);
FileHelper.DeleteDirectory(@"C:\Temp\OldData");

// 加密
string sha256 = CryptoHelper.ComputeSha256Hash("hello");
string md5 = CryptoHelper.ComputeMd5Hash("hello");

// 网络
await NetworkHelper.DownloadFileAsync("https://example.com/file.zip", @"D:\Download\file.zip");

System — 系统信息

硬件信息 — SystemHardwareInfo
string ports = SystemHardwareInfo.GetPort();          // 串口列表
string disks = SystemHardwareInfo.GetDiskInfo();       // 磁盘信息
string cpu = SystemHardwareInfo.GetCpuInfo();          // CPU 信息
string os = SystemHardwareInfo.GetOsInfo();            // 操作系统
string network = SystemHardwareInfo.GetNetworkAdapterInfo(); // 网络适配器
机器码与授权 — MachineCodeGenerator
// 生成 24 位机器码(CPU + 硬盘 + 网卡)
string machineCode = MachineCodeGenerator.GenerateMachineCode();

// SHA256 哈希
string hash = MachineCodeGenerator.GetMachineCodeHash();

// 验证授权码(Base64 编码的 "机器码|过期日期")
var (isValid, expiryDate) = MachineCodeGenerator.ValidateLicenseKey(licenseKey, machineCode);

Plc — PLC 通信

MelsecPlcHelper

支持三菱 FX 系列(X/Y/M/S/T/C/D 元件),ASCII 协议串口通信。

// 连接 PLC
MelsecPlcHelper.Connect("COM1", baudRate: 9600);

// 读取
bool x0 = MelsecPlcHelper.ReadX(0);
bool y1 = MelsecPlcHelper.ReadY(1);
bool m10 = MelsecPlcHelper.ReadM(10);
short d100 = MelsecPlcHelper.ReadD(100);
short t5 = MelsecPlcHelper.ReadT(5);

// 写入
MelsecPlcHelper.WriteY(1, true);
MelsecPlcHelper.WriteM(10, false);
MelsecPlcHelper.WriteD(100, 12345);

// 轮询 D 寄存器
MelsecPlcHelper.OnDataUpdated += snapshot => {
    Console.WriteLine($"D100 = {snapshot.D[100]}");
};
MelsecPlcHelper.StartPolling(new[] { 100, 101, 102 }, intervalMs: 100);

// 日志监听
MelsecPlcHelper.OnLog += (msg, level) => Console.WriteLine($"[{level}] {msg}");

// 断开
MelsecPlcHelper.Shutdown();

Application — 应用层服务

MES 通信 — MesClient
// GET 请求
string result = await MesClient.SendGetRequestAsync(
    "https://mes-server/api", "production/orders", apiKey: "your-token");

// POST 请求
var data = new { OrderId = "P001", Quantity = 100 };
string response = await MesClient.SendPostRequestAsync(
    "https://mes-server/api", "production/report", data, apiKey: "your-token");
桌面快捷方式 — ShortcutCreator
ShortcutCreator.CreateShortcut("我的应用", @"D:\App\MyApp.exe", arguments: "-start");
开机启动 — AutoStartManager
AutoStartManager.EnsureAutoStart("MyApp", @"D:\App\MyApp.exe", enable: true);
AutoStartManager.EnsureAutoStart("MyApp", null, enable: false);  // 取消
安装验证 — VerifyInstallation
// 检查软件是否安装
bool installed = VerifyInstallation.IsSoftwareInstalled("MyApp", version: "2.0");

// 安装程序(需用户确认委托)
VerifyInstallation.Notify = (title, msg) => MessageBox.Show(msg, title) == DialogResult.OK;
await VerifyInstallation.InstallAsync("安装 MyApp", @"D:\Installer", "setup.exe");

发布

dotnet pack -c Release
dotnet nuget push bin\Release\QianTools.*.nupkg --source https://api.nuget.org/v3/index.json
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.8.0 47 10/24/2026
2.7.1 48 10/23/2026
2.7.0 328 7/30/2026
2.6.5 127 7/26/2026
2.6.1 127 7/26/2026
2.6.0 127 7/21/2026
2.5.0 129 7/21/2026
2.4.0 123 7/21/2026
2.3.5 149 5/19/2026
2.3.4 119 5/19/2026
2.3.3 136 5/19/2026
2.3.2 133 5/16/2026
2.3.1 138 5/15/2026
2.3.0 138 5/15/2026
2.1.1 217 12/23/2025
1.1.3 210 12/23/2025
1.1.0 218 12/23/2025
1.0.0 203 7/20/2024