views:

100

answers:

3

AssemblyName.GetAssemblyName("").Version.ToString() will give the version number ,,but i need to get version number from registry

registry path is under MY computer -> HKEY_LOCAL_MACHINE->SOFTWARE->

leaf->monitor here current version file is there,,from there we need to

fetch version number

A: 

Here's the class that allows you to read and write windows registy. All you need to do with it is to find the proper value to read.

RaYell
any other coding is there?
peter
+1  A: 

you can use the following code to get the version key value:

RegistryKey key= Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("leaf").OpenSubKey("monitor ");
string version = key.GetValue("version");

Edit: check now :)

Wael Dalloul
here key and version are integer type or?
peter
Error 1 The name 'key' does not exist in the current context F:\versionnum\versionnum\Form1.cs 40 14 versionnumError 2 The name 'version' does not exist in the current context F:\versionnum\versionnum\Form1.cs 41 13 versionnum
peter
i am getting those errors
peter
i inserted Microsoft.Win32; namespace here
peter
+3  A: 

Wael's code works fine, but there's a couple other (slightly syntactically cleaner) ways to do it, for example:

  • OpenSubKey knows how to open several subkeys at once:

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\leaf\monitor");

  • Additionally, if you only need to get a value, which your question asks about, it's even simpler (and this allows you to set a fallback value if your target key doesn't exist, avoiding a thrown exception if you use OpenSubKey):

string version = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\leaf\monitor", "version", "0");

Factor Mystic
thanks for your information
peter