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" />
                    
Directory.Packages.props
<PackageReference Include="ZYW.STRHelper" />
                    
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 ZYW.STRHelper --version 1.19.0
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=ZYW.STRHelper&version=1.19.0
                    
Install as a Cake Tool

ZYW.STRHelper - 字符串与通用辅助工具库

NuGet Version NuGet Downloads

介绍

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>:根据枚举生成 SQL CASE 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 文件夹)

注意事项

  1. 日志写入需要程序运行目录下 Log 文件夹的写权限;写入失败会被静默忽略(仅 Debug.WriteLine 输出)。
  2. 日志去重按“同一个日志文件内、连续相同内容”生效,不同前缀的日志文件互不影响。
  3. 随机字符串允许字符重复,长度可超过字符集大小;长度为负数会抛 ArgumentOutOfRangeException。
  4. GetAppSettingsItem 在键缺失且默认值非空时会回写配置;默认值为空时不写文件。

许可证

本项目采用 MIT 许可证开源。

Product 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.

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
1.19.0 92 11/4/2026
1.17.5 128 5/25/2026
1.16.5 121 5/13/2026
1.16.4 116 5/13/2026
1.16.3 136 3/13/2026
1.16.2 122 3/13/2026
1.16.1 130 3/11/2026
1.16.0 126 3/11/2026