ZYW.STRHelper
1.19.0
dotnet add package ZYW.STRHelper --version 1.19.0
NuGet\Install-Package ZYW.STRHelper -Version 1.19.0
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="ZYW.STRHelper" Version="1.19.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZYW.STRHelper" Version="1.19.0" />
<PackageReference Include="ZYW.STRHelper" />
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 ZYW.STRHelper --version 1.19.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZYW.STRHelper, 1.19.0"
#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 ZYW.STRHelper@1.19.0
#: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=ZYW.STRHelper&version=1.19.0
#tool nuget:?package=ZYW.STRHelper&version=1.19.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ZYW.STRHelper - 字符串与通用辅助工具库
介绍
ZYW.STRHelper 是一个轻量级的 .NET 辅助类库,提供随机数据生成、日志写入、文件操作、配置文件读写、枚举转换等通用功能,简化日常开发中的重复编码工作。
功能特性
1. 随机数据生成
- 生成指定长度的随机字符串(数字 + 大小写字母,排除 0、o、O、l、L 等易混淆字符)
- 生成指定长度的随机数字字符串
- 生成 32 位无分隔符的 GUID 字符串
- 基于
RandomNumberGenerator(加密安全随机数)生成,并采用拒绝采样消除取模偏差 - 长度不受字符集大小限制(允许字符重复),不会死循环
2. 日志与文件操作
WriteLog扩展方法:按Log/yyyyMM/[前缀]dd.txt分目录分文件存储,同一文件连续的相同内容自动去重,线程安全,写入异常不会影响业务SaveToTxt:字符串直接保存为 TXT 文件,自动创建目录,支持指定编码CreateDirectory/DeleteDirectory:目录创建(幂等)与递归删除(自动清除只读属性)
3. 配置与枚举
GetAppSettingsItem/SetAppSettingsItem:基于app.config/*.exe.config的键值读写,内置写锁GetEnumSqlCase<T>:根据枚举生成 SQLCASE WHEN语句GetEnumDescription:读取枚举的Description特性
安装
Install-Package ZYW.STRHelper
或:
dotnet add package ZYW.STRHelper
快速开始
命名空间
using Helper;
生成随机数据
string randomStr = STRHelper.GetRandomString(8); // 示例输出:9s7B8k2P
string randomNum = STRHelper.GetRandomNumString(6); // 示例输出:879201
string guid = STRHelper.GetGuidString(); // 示例输出:c7b9f8e7a6d543b2a1c0d9e8f7a6b5c4
日志写入
// 写入日志(Log/yyyyMM/dd.txt)
"用户登录成功:admin".WriteLog();
// 指定文件名前缀(写入 Log/yyyyMM/PayLog_dd.txt)
"支付回调".WriteLog("PayLog_");
// 带时间分割线
"系统启动完成".WriteLog(tagDateTime: true);
// 不记录
"测试日志".WriteLog(RecordLog: false);
保存到 TXT 文件
"Hello, STRHelper!".SaveToTxt(@"D:\test\a.txt");
"中文内容".SaveToTxt(@"D:\test\gbk.txt", Encoding.GetEncoding("GBK"));
枚举转 SQL
public enum OrderState
{
[Description("待付款")] WaitPay = 1,
[Description("已完成")] Finished = 2
}
string sqlCase = "State".GetEnumSqlCase<OrderState>("StateName");
// Case State
// when 1 then '待付款'
// when 2 then '已完成'
// else '' end StateName
详细 API 说明
| 方法 | 说明 |
|---|---|
GetRandomString(int len) |
加密安全随机字符串(不含易混淆字符) |
GetRandomNumString(int len) |
加密安全随机数字字符串 |
GetGuidString() |
32 位无分隔符 GUID |
WriteLog(this string content, string CustomName = "", bool tagDateTime = false, bool RecordLog = true) |
写日志,CustomName 为文件名前缀 |
SaveToTxt(this string str, string TxtFileName) |
保存为 TXT(UTF-8 无 BOM) |
SaveToTxt(this string str, string TxtFileName, Encoding encoding) |
保存为 TXT 并指定编码 |
CreateDirectory(this string DirectoryPath) |
目录不存在则创建 |
DeleteDirectory(this string DirectoryPath) |
递归删除目录(清除只读属性) |
GetAppSettingsItem(this string keyName, string defautValue = "") |
读配置,缺失时返回默认值并写回 |
SetAppSettingsItem(this string keyName, string newKeyValue) |
写配置 |
GetEnumSqlCase<T>(this string FiledName, string CaseAsName = "") |
生成枚举的 SQL CASE 语句(自动转义单引号) |
GetEnumDescription(this Enum enumValue) |
获取枚举 Description,无特性/组合值时返回枚举名 |
兼容性
- 目标框架:
netstandard2.0 - 可使用:.NET Framework 4.6.1+(建议 4.7.2+)、.NET Core 2.0+、.NET 5/6/7/8/9/10
- 路径拼接使用
Path.Combine,Windows / Linux 均可用(日志默认写入程序运行目录下Log文件夹)
注意事项
- 日志写入需要程序运行目录下
Log文件夹的写权限;写入失败会被静默忽略(仅Debug.WriteLine输出)。 - 日志去重按“同一个日志文件内、连续相同内容”生效,不同前缀的日志文件互不影响。
- 随机字符串允许字符重复,长度可超过字符集大小;长度为负数会抛
ArgumentOutOfRangeException。 GetAppSettingsItem在键缺失且默认值非空时会回写配置;默认值为空时不写文件。
许可证
本项目采用 MIT 许可证开源。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- System.Configuration.ConfigurationManager (>= 10.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.