添加项目文件。
This commit is contained in:
31
口罩泄露定制款/Data/IDGenerator.cs
Normal file
31
口罩泄露定制款/Data/IDGenerator.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.IO;
|
||||
|
||||
namespace 口罩泄露定制款
|
||||
{
|
||||
public class IDGenerator
|
||||
{
|
||||
private static int currentID;
|
||||
private static readonly object lockObject = new object();
|
||||
private const string prefix = "ID-";
|
||||
private const string filePath = "currentID.txt";
|
||||
|
||||
static IDGenerator()
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var idString = File.ReadAllText(filePath);
|
||||
int.TryParse(idString, out currentID);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GenerateID()
|
||||
{
|
||||
lock (lockObject)
|
||||
{
|
||||
currentID++;
|
||||
File.WriteAllText(filePath, currentID.ToString());
|
||||
return $"{prefix}{currentID:D5}"; // 生成形如 "ID-00001" 的编号
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user