Thursday 22 May 2014

Works with Windows registry

Today I am going to explain how we can fetch required data from windows registry like unistalltion String,
In one of my project I followed below steps to get uninstalltion String to unistall required softwares.

If you have OS 32 then you need to read this registry to get data
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall

or if you have OS 64
Then you need to check both
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall
and
HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall

First way to accomplish this work
Step 1=

First we need to save registry data into a text file,you can execute this string from java class

Runtime.getRuntime().exec("reg export HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall "+"\""+ tempFile + "\"");

Step 2=
This text file consist some unreadable character so first we need to remove those,
you can put your logic to create a new file with the help of tempFile after removing unreadable character
I  used this code to get readable file

InputStreamReader input = null;
OutputStreamWriter output = null;
try {
input = new InputStreamReader(new FileInputStream(new File(tempFile)));
output = new OutputStreamWriter(new FileOutputStream(readableFile));
while (true) {
int ch = input.read();
if (ch < 0)
break;
else if ('\n' == (char) ch || ch >= 32 && ch <= 127) {
output.write(ch);

}
}


Step 3=
Now you have readable file which have data like
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\3519-9895-1195-8563]
"DisplayName"="testSoft"
"DisplayIcon"="C:\\Program Files (x86)\\testSoft-03\\.install4j\\i4j_extf_10_3zfp0v_1at2cp6.ico"
"UninstallString"="C:\\Program Files (x86)\\ntestSoft\\bin\\uninstall.exe"
"Publisher"="testSoft, Inc."
"URLInfoAbout"="http://testSoft.com"
"InstallLocation"="C:\\Program Files (x86)\\testSoft"
"DisplayVersion"="2.7.2-03"
"VersionMajor"=dword:00000002
"VersionMinor"=dword:00000007

So from this file you can fetch required value and to unistall any software  you can fetch value of UninstallString and execute  below query
Runtime.getRuntime().exec("+softPath+").waitFor();
where softPath=C:\\Program Files (x86)\\testSoft\\bin\\uninstall.exe

Second way 

You can  also use wmic to get registry data,but there is some issue with that when you use this you can see, it doesn't have data of all softwares which installed on your system,
No doubt it easy, so to get complete software data you can combine  both way in one to accomplish your work

To uninstall any software with the help of wmic

you can follow above two steps to generate readable format,you just need to change string query of first step
Runtime.getRuntime().exec("cmd /c wmic product get description >"+"\""+ tempFile + "\"");
I have used description to get software name
after that to uninstall any software  I just need to execute this String
Runtime.getRuntime().exec("wmic product where name=\'"+ description + "\' call uninstall").waitFor();

Hope it helps any one

No comments:

Post a Comment