Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 112
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Windows CD Key auslesen


Autor: Gast
Sprache: C#
Bewertung:
8.24 (5 votes)
Anzahl der Aufrufe: 15621
  
Kick it on dotnet-kicks.de  

Beschreibung:

Liest aus der Windows Registry den Windows CD Key aus.

Abgelegt unter: microsoft, windows xp, windows 2000, windows 2003, cd key, Registry.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace CD_Key
{
    public class Win32
    {
        #region Properties
        /// <summary>
        /// Reads the Windows CD key from the registry and returns it as string separated by '-' chars.
        /// </summary>
        public static string WindowsCDKey
        {
            get
            {
                RegistryKey rKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
                byte[] rpk = (byte[])rKey.GetValue("DigitalProductId", new byte[0]);

                string strKey = "";

                const int iRPKOffset = 52;
                const string strPossibleChars = "BCDFGHJKMPQRTVWXY2346789";
                int i = 28;

                do
                {
                    long lAccu = 0;
                    int j = 14;

                    do
                    {
                        lAccu *= 256; lAccu += Convert.ToInt64(rpk[iRPKOffset + j]);

                        rpk[iRPKOffset + j] =
                            Convert.ToByte(
                            Convert.ToInt64(Math.Floor((float)lAccu / 24.0f)) & Convert.ToInt64(255)
                            );

                        lAccu %= 24;

                        j -= 1;
                    }
                    while (j >= 0);

                    i -= 1;
                    strKey = strPossibleChars[(int)lAccu].ToString() + strKey;

                    if ((0 == ((29 - i) % 6)) && (-1 != i))
                    {
                        i -= 1;

                        strKey = "-" + strKey;
                    }
                }
                while (i >= 0);

                return strKey;
            }
        }
        
        /// <summary>
        /// Reads the Windows CD key from the registry and returns it as string array.
        /// </summary>
        public static string[] WindowsCDKeyParts
        {
            get
            {
                string[] strKeyParts = WindowsCDKey.Split('-');
                return strKeyParts;
            }
        }
        #endregion
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

Marcel Kloubert schrieb am:  22.09.2006 00:13:38

ACHTUNG: Der Namespace hat fälschlicherweise ein Leerzeichen! Dieses am besten mit einem Unterstrich ersetzen!
Jan Welker schrieb am:  22.09.2006 06:04:47

Ich habe den Unterstrich eingefügt.


schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.