0

i want to use ProductId of windows machine so i have written this code but this code is unable to get the ProductId from the registry when running on windows server 2008 when i have visited registry the ProductId is there in Registry

string[] names = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValueNames();
foreach (string s in names)
{
  listBox1.Items.Add(s);
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
Nitin Bourai
  • 430
  • 7
  • 20
  • What exactly do you mean its "unable" to get the ProductID? When this code is run, what is contained in the string array **names** – Security Hound Jan 04 '12 at 13:59

2 Answers2

1

I found out the reason this happens. It's because your program is 32-bit and your system is 64-bit. 32-bit programs access the 32-bit portion of the registry, and this key is not present in the 32-bit portion, which is located in:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion

Compile your program in 64-bit and it's will work.

Ben
  • 51,770
  • 36
  • 127
  • 149
Cicero
  • 11
  • 1
0

Make sure the account this code is running under has permissions to read from the registry (or at least that location in the registry).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • i am Able to Get all the Information in that section of registry except ProductId ,i have administrator rights what can be the reason for this – Nitin Bourai Jan 04 '12 at 11:10
  • @NitinBourai - When debugging does `names` contain it? – Oded Jan 04 '12 at 11:20
  • when i am running the code in OS other than windows 2008 the names contain the ProductId but when i am running on 2008 names does not contain ProductId is this some kind of security in 2008 ? – Nitin Bourai Jan 04 '12 at 11:50
  • @NitinBourai - I am not aware of such a feature. Does the key exist in the registry on that computer? – Oded Jan 04 '12 at 11:51
  • @NitinBourai - Then I can only assume there are permission issues. – Oded Jan 04 '12 at 12:07