生成唯一文件名不同方法

测试一

private void TestUnid()
{
Parallel.For(1, 100, (i) =>
{
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
Random rnd = new Random();
Console.WriteLine("fileName=" + fileName + rnd.Next(1000, 100000));
});
}



输出:


fileName=2016102614594469808


fileName=2016102614594469808


fileName=2016102614594469808


...重复的


fileName=201610261459446330


fileName=201610261459446330


fileName=201610261459446330


fileName=201610261459446330


...重复的


fileName=2016102614594411511


fileName=2016102614594411511


fileName=2016102614594411511


fileName=2016102614594411511


结论:可以看出有大量重复的文件名,原因是在并行时,Random用的默认种子是一样的,所以得到的随机数重复,



测试二

private static int iCount;

private void TestUnid()
{
Parallel.For(1, 100, (i) =>
{
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
Random rnd = new Random();
Interlocked.Increment(ref iCount);
Console.WriteLine("fileName=" + fileName + rnd.Next(10, 10000)+"_"+iCount.ToString());
});
}



输出:


fileName=201610261502447236_1


fileName=201610261502447236_3


fileName=201610261502447236_5


fileName=201610261502447236_6


fileName=201610261502447236_8


fileName=201610261502447236_9


fileName=201610261502447236_10


fileName=201610261502447236_11


fileName=201610261502447236_12


fileName=201610261502447236_13


fileName=201610261502447236_14


fileName=201610261502447236_15


fileName=201610261502447236_16


fileName=201610261502447236_17


fileName=201610261502447236_4


fileName=201610261502447236_19


fileName=201610261502447236_18


fileName=201610261502447236_21


fileName=201610261502447236_22


fileName=201610261502447236_23


fileName=201610261502447236_24


fileName=201610261502447236_25


fileName=201610261502447236_26


fileName=201610261502447236_27


fileName=201610261502447236_28


fileName=201610261502447236_29


fileName=201610261502447236_2


fileName=201610261502447236_31


fileName=201610261502447236_7


fileName=201610261502447236_33


fileName=201610261502447236_34


fileName=201610261502447236_35


fileName=201610261502447236_36


fileName=201610261502447236_20


fileName=201610261502447236_30


fileName=201610261502447236_32


fileName=201610261502447236_37


fileName=201610261502447236_41


fileName=201610261502447236_43


fileName=201610261502447236_44


fileName=201610261502447236_45


fileName=201610261502447236_46


fileName=201610261502447236_47


fileName=201610261502447236_48


fileName=201610261502447236_49


fileName=201610261502447236_50


fileName=201610261502447236_51


fileName=201610261502447236_42


fileName=201610261502447236_53


fileName=201610261502447236_54


fileName=201610261502447236_55


fileName=201610261502447236_56


fileName=201610261502447236_57


fileName=201610261502447236_58


fileName=201610261502447236_59


fileName=201610261502447236_60


fileName=201610261502447236_61


fileName=201610261502447236_62


fileName=201610261502447236_63


fileName=201610261502447236_64


fileName=201610261502447236_65


fileName=201610261502447236_39


fileName=201610261502447236_66


fileName=201610261502447236_40


fileName=201610261502447236_68


fileName=201610261502447236_38


fileName=201610261502447236_67


fileName=201610261502447236_69


fileName=201610261502447236_52


fileName=201610261502447759_73


fileName=201610261502447759_74


fileName=201610261502447759_75


fileName=201610261502447759_76


fileName=201610261502447759_77


fileName=201610261502447759_78


fileName=201610261502447759_79


fileName=201610261502447759_80


fileName=201610261502447759_81


fileName=201610261502447759_70


fileName=201610261502447759_82


fileName=201610261502447759_84


fileName=201610261502447759_85


fileName=201610261502447759_86


fileName=201610261502447759_87


fileName=201610261502447759_83


fileName=201610261502447759_71


fileName=201610261502447759_88


fileName=201610261502447759_72


fileName=201610261502447759_90


fileName=201610261502447759_91


fileName=201610261502447759_92


fileName=201610261502447759_93


fileName=201610261502447759_94


fileName=201610261502447759_89


fileName=201610261502447759_95


fileName=201610261502447759_96


fileName=201610261502447759_97


fileName=201610261502453063_98


fileName=201610261502453063_99


结论:可以看出没有重复的,但在这里随机数也还是没用,不重复是靠iCount来控制的



测试三



private void TestUnid()
{
Parallel.For(1, 100, (i) =>
{
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss");


Random rnd = new Random(Guid.NewGuid().GetHashCode());
Console.WriteLine("fileName=" + fileName + rnd.Next(10, 1000000));
});
}



输出:


fileName=20161026151027973826


fileName=20161026151027179385


fileName=20161026151027445591


fileName=20161026151027847317


fileName=20161026151027606114


fileName=20161026151027929078


fileName=20161026151027304060


fileName=20161026151027690171


fileName=20161026151027611574


fileName=20161026151027479382


fileName=20161026151027721579


fileName=2016102615102738259


fileName=2016102615102734888


fileName=20161026151027315651


fileName=20161026151027724837


fileName=20161026151027547173


fileName=20161026151027314361


fileName=20161026151027235100


fileName=20161026151027463807


fileName=20161026151027912844


fileName=20161026151027260381


fileName=20161026151027984721


fileName=20161026151027293500


fileName=20161026151027123672


fileName=20161026151027597298


fileName=20161026151027885499


fileName=20161026151027408799


fileName=20161026151027196271


fileName=20161026151027472364


fileName=20161026151027844070


fileName=20161026151027789116


fileName=20161026151027643316


fileName=20161026151027502302


fileName=20161026151027448703


fileName=2016102615102751061


fileName=20161026151027151289


fileName=20161026151027366014


fileName=20161026151027517970


fileName=20161026151027277171


fileName=20161026151027861437


fileName=20161026151027180729


fileName=20161026151027910238


fileName=20161026151027864665


fileName=20161026151027529615


fileName=20161026151027421393


fileName=20161026151027225208


fileName=20161026151027118632


fileName=20161026151027453933


fileName=20161026151027781073


fileName=20161026151027435784


fileName=20161026151027259237


fileName=20161026151027600569


fileName=20161026151027521430


fileName=20161026151027237649


fileName=20161026151027933160


fileName=20161026151027396719


fileName=20161026151027685772


fileName=20161026151027701230


fileName=20161026151027400333


fileName=20161026151027410161


fileName=20161026151027788633


fileName=20161026151027936531


fileName=20161026151027522519


fileName=20161026151027849716


fileName=20161026151027590020


fileName=20161026151027555132


fileName=20161026151027259429


fileName=20161026151027567186


fileName=20161026151027390216


fileName=20161026151027614342


fileName=2016102615102854752


fileName=20161026151028173374


fileName=20161026151028991435


fileName=2016102615102894246


fileName=20161026151028497667


fileName=20161026151028365669


fileName=20161026151028808273


fileName=20161026151028568480


fileName=20161026151028686578


fileName=20161026151028627480


fileName=20161026151028248219


fileName=20161026151028691944


fileName=201610261510284772


fileName=20161026151028346370


fileName=20161026151028817242


fileName=20161026151028340723


fileName=20161026151028236019


fileName=20161026151028638904


fileName=20161026151028248779


fileName=20161026151028123635


fileName=20161026151028344443


fileName=20161026151028699403


fileName=20161026151028349303


fileName=20161026151028118220


fileName=20161026151028232106


fileName=20161026151028185281


fileName=2016102615102891660


fileName=20161026151028933221


fileName=20161026151028167604


结论:这里面应该也是没有重复的,但有重复的可能,因为虽然Random的种子每次不一样了,但生成随机数还是有可能会有重复,但概率比较小了,



测试四


private static int iCount;

private void TestUnid()
{
Parallel.For(1, 100, (i) =>
{
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
Interlocked.Increment(ref iCount);
Random rnd = new Random(Guid.NewGuid().GetHashCode());
Console.WriteLine("fileName=" + fileName + iCount.ToString().PadRight(4,'0')+rnd.Next(1, 10000).ToString().PadLeft(4,'0'));
});
}


输出:


fileName=2016102615421634001080


fileName=2016102615421633007073


fileName=2016102615421635000421


fileName=2016102615421636006721


fileName=2016102615421627005785


fileName=2016102615421638000321


fileName=2016102615421642003512


fileName=2016102615421641005752


fileName=2016102615421643008674


fileName=2016102615421644004218


fileName=2016102615421645008183


fileName=2016102615421646003962


fileName=2016102615421647000283


fileName=2016102615421648003970


fileName=2016102615421649005367


fileName=2016102615421651004208


fileName=2016102615421650001274


fileName=2016102615421652008767


fileName=2016102615421653000678


fileName=2016102615421654007918


fileName=2016102615421655002593


fileName=2016102615421656002064


fileName=2016102615421657009178


fileName=2016102615421658005146


fileName=2016102615421659006539


fileName=2016102615421660003764


fileName=2016102615421661008740


fileName=2016102615421640002590


fileName=2016102615421663000390


fileName=2016102615421664004351


fileName=2016102615421665000917


fileName=2016102615421666005014


fileName=2016102615421637000092


fileName=2016102615421639009017


fileName=2016102615421662001313


fileName=2016102615421667004809


fileName=2016102615421672006933


fileName=2016102615421673007296


fileName=2016102615421674009097


fileName=2016102615421675002749


fileName=2016102615421676006875


fileName=2016102615421677008682


fileName=2016102615421678005366


fileName=2016102615421669005274


fileName=2016102615421670002655


fileName=2016102615421679001878


fileName=2016102615421680004663


fileName=2016102615421681000694


fileName=2016102615421682007521


fileName=2016102615421683001165


fileName=2016102615421684007344


fileName=2016102615421685008041


fileName=2016102615421668002134


fileName=2016102615421686002166


fileName=2016102615421671003766


fileName=2016102615421687001968


fileName=2016102615421688007910


fileName=2016102615421689001718


fileName=2016102615421690003355


fileName=2016102615421691007049


fileName=2016102615421692006476


fileName=2016102615421693007589


fileName=2016102615421694003435


fileName=2016102615421695007512


fileName=2016102615421696006017


fileName=2016102615421697008858


fileName=2016102615421698007807


fileName=2016102615421699001709


结论:这个是没有重复的,生成的随机数种子也不同,而且带上iCount保证了唯一。



测试五

private void TestUnid()
{
Parallel.For(1, 100, (i) =>
{
//不用ToString("N"),默认的是"D",里面带"-"
var fileName = Guid.NewGuid().ToString("N");
Console.WriteLine("fileName=" + fileName);
});
}


输出:


fileName=48f5445240ab4e16b9243f833606c765


fileName=3001bccb4ecc4672a1492bf7ddb7b6aa


fileName=2a9091388671447b82dc38a8503d429e


fileName=3399220040c04d319774ed1891eddb2f


fileName=9716c75fd8a74ea38833b7fb126c32a2


fileName=e1062e22a2d4449395e39f86149bbfe8


fileName=8ab0e5fef7df44c29ee9a79a4f139630


fileName=e022a9af93b3451c9bf6fceb7e4a2c0b


fileName=4edfb58d071043dca5d574508696ed4a


fileName=a8b05d2ef92c422a8ebb128d0102af4d


fileName=5382d16c9eb140d192c7d083b05e19a9


fileName=14bde8b6f3cc42b2b3331c9c61a5d3f6


fileName=80f6848bbb224947a2b2326039f91050


fileName=dd3c8791bbbb4f17ba0910f67af857d6


fileName=35f348299803439b9e5588681d4e55bf


fileName=e6f208c012fc438d8d09c72a6272fc45


fileName=99371f05de894160a8ee6b8776aa5492


fileName=01336494f3804bdea8da71d552756052


fileName=82c35aa705a34c0cb1378f145eb4c69f


fileName=92315ae5910e41828296f0f468808a5d


fileName=f0f1e6ebaad6410aaa64e997a0f28b5a


fileName=477c9c50edc24c339e22ae997f8c8674


fileName=814f47f6b9ac47ed8fe9027bf4db9bde


fileName=8adc22da263941588a7644e65176682d


fileName=116ee790785847f7b9129f7fbf1f2ac2


fileName=227e6cf1bf334d0da3a57c46c655edcd


fileName=47b5fd5e03d84f4b945fa9d47a199174


fileName=900e240a183c461bba3c28e871341fb9


fileName=bbbd95ccd0b143dfbe5adc19292c0bf4


fileName=28450dfff97045c882f740a511b0d4fe


fileName=8ce2a10345f14bb987367653345e931d


fileName=7e90485d1dd84e638a68605883305afd


fileName=1d46823cef004254b4d4705ccaa94d6b


fileName=6c3d82c142954f48a754bf0a8e403812


fileName=1cd674231cb34a7f9c5d3595d0947daa


fileName=3ec4b8f1f4eb4b8897a0d369d8f8725e


......


这个应该也是没有重复的,利用的是Guid的唯一性,这种方法比较省事,但是不带时间信息在里面

所以,要生成真正唯一的文件名,要用最后二种方法

--- end ---