今天,在C#中使用Guid类型创建随机ID时遇到问题。
当我想要随机ID时。我用:
obj.ID = new Guid(txtID.Text); //ID type Guid
但是在运行程序时。当我刷卡时,它会自动生成1个id随机。
我想要刷卡时。它将随机ID并保留此值,直到我删除它。
修改 我的英语不好。对不起。
这里我的代码生成Guid并将值设置为textbox:
mReader.CurrentCardIDBlock1 = System.Guid.NewGuid().ToString("N");
mReader.CurrentCardIDBlock2 = System.Guid.NewGuid().ToString("N");
txtID.Text = mReader.CurrentCardIDBlock1.ToString();
String strCompont = txtID.Text;
onWriteDataBlock1(strCompont); //Write data to mifare card in block 1 sector 14
MessageBox.Show("Write data success");
如果swipe card 1
> random id[any value]
。
继续swipe card 1
> random id[any value]
。 => incorrect
。
我想要swipe card 1
> random id[value 1]
,如果我拿卡留下RFID阅读器再放一次。
它直到我random id[value 1]
的返回值为card 1
。
滑动card 2
> random id[any value]
。 Swipe card 3
> random id[any value]
。 =>没关系。
我觉得这很容易想象。 感谢@Preston Guillot。
谢谢!!!
答案 0 :(得分:2)
Just use Guid.NewGuid() instead explicit instanciation, it will generate 99.9 % unique ID.