C# 如何创建序列号生成器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1732480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:23:03  来源:igfitidea点击:

How to create serial number generator?

c#licensinglicense-key

提问by nir

How can I program a Serial Number Generator that generates serial numbers for some existing software?

如何编写序列号生成器来为某些现有软件生成序列号?

回答by Jay Riggs

You don't state any specific requirements but you can use a GUID.

您没有说明任何特定要求,但可以使用 GUID。

Guid mySerialNumber = Guid.NewGuid();

回答by Ash

I have used this CodeProject Articlebefore when I've needed to do the same thing for projects I have worked on recently. I found it very detailed and while I didn't use everything in the sample I did get a lot out of it. (I have no affiliation with CodeProject or the author)

当我需要为我最近从事的项目做同样的事情时,我以前使用过这篇CodeProject 文章。我发现它非常详细,虽然我没有使用示例中的所有内容,但我确实从中受益匪浅。(我与 CodeProject 或作者没有任何关系)

Additionally there is a library which gives you all this functionality complete with license file replacement and heaps of other features, but its not free, and unfortunately I couldn't remember the link. I'm sure a bit of Googgling and you'll find it.

此外,还有一个库可以为您提供所有这些功能,包括许可证文件替换和大量其他功能,但它不是免费的,不幸的是我不记得链接了。我敢肯定,你会用一些谷歌搜索找到它。

回答by Traveling Tech Guy

Try a number you can test using Luhn's algorithm. That way, you can make it long an inscrutable, yet still easily confirmed. The article contains a link to a C# implementation.

尝试使用Luhn 算法可以测试的数字。那样的话,你可以让它长得高深莫测,但仍然很容易确认。该文章包含指向 C# 实现的链接。

回答by vijay

public static string GetSerialKeyAlphaNumaric(SNKeyLength keyLength)
{
    string newSerialNumber = "";
    string SerialNumber=Guid.NewGuid().ToString("N").Substring(0, (int)keyLength).ToUpper(); 
    for(int iCount=0; iCount < (int) keyLength   ;iCount +=4)
        newSerialNumber = newSerialNumber + SerialNumber.Substring(iCount, 4) + "-";
    newSerialNumber = newSerialNumber.Substring(0, newSerialNumber.Length - 1); 
    return newSerialNumber;
}