operating system - get installed version of an application using c# -
i installed version of application (say, myapp) using c#. much, 1. create 'set up' myapp of version 5.6 2. install myapp.
i create application (say versiontracker)to version of installed applications. if pass name 'myapp' version '5.6'. if application adobe reader installed in system, want version of adobe reader if pass 'adobe reader'.
i need know how build 'versiontracker'
the first , important thing not applications save version somewhere in system. honest, few of them that. place should windows registry. of installed applications put installation data following place:
hkey_local_machine\software\microsoft\windows\currentversion\uninstall however, it's not easy - on 64bit windows, 32bit (x86) applications save installation data key, is:
hkey_local_machine\software\wow6432node\microsoft\windows\currentversion\uninstall in these keys there many keys, of them have got "easy-readable" name, such google chrome, of them got names such {63e5cdbf-8214-4f03-84f8-cd3ce48639ad}. must parse these keys application , start looking application names. there in displayname value, it's not true. version of application in displayversion value, installers use values, such inno setup: setup version, ... application have version written in name, it's possible application version in displayname value.
note: it's not easy parse these registry keys , values , "pick" correct values. not installers save application data these keys, of them not save application version there, etcetera. however, it's usual application use these registry keys. [source: stackoverflow: detecting installed programs via registry, browsing own registry]
alright, when know should look, have program in c#. won't write application you, i'll tell classes should use , how to. first, need these:
using system; using microsoft.win32; to hkey_local_machine, create registrykey this:
registrykey baseregistrykey = registry.localmachine; now need define subkeys:
string subkey = "software\\microsoft\\windows\\currentversion\\uninstall"; // or "software\\wow6432node\\microsoft\\windows\\currentversion\\uninstall" now need go subkey, create new registrykey:
registrykey uninstallkey = baseregistrykey.opensubkey(subkey); now need go thru subkeys there, first names of subkeys:
string[] allapplications = uninstallkey.getsubkeynames(); now you must go thru subkeys yourself, 1 one, creating new registry key (you don't have to, i'll it):
registrykey appkey = baseregistrykey.opensubkey(subkey + "\\" + applicationsubkeyname); where applicationsubkeyname name of subkey you're checking. recommend foreach statement, helps (you must have experience c# already, i'm not going tell how use foreach here).
now check application's name , compare name of desired application (you cannot rely on subkey name, because, said, can called example {63e5cdbf-8214-4f03-84f8-cd3ce48639ad}, must check name here):
string appname = (string)appkey.getvalue("displayname"); if it's correct application (you must check yourself), find version:
string appversion = (string)appkey.getvalue("displayversion"); et voilĂ , have version. @ least there's 60 - 80% chance have...
remember! if key or value doesn't exist, method returns null. remember check if returned value null everytime, otherwise application crash.
where find more? the code project: read, write , delete registry c#
i hope helped you. , if wanted know else , didn't understand question, then, please, ask better next time. :)
Comments
Post a Comment