31 lines
838 B
C#
31 lines
838 B
C#
|
|
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" 的编号
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|