In normal testing work, software is often installed and uninstalled, Which is cumbersome and tiring. Installation and uninstallation can be automated. For software installation, we can use the automation framework and click Next to install the software automatically. Uninstall software. We can use the msiexec command line tool to automatically uninstall the software.
Reading directory
- Run the msiexec command to uninstall the software.
- Search for ProductCode in the Registry
- C # automatically uninstall software
- C # search for ProductCode in the Registry
- Download complete source code
Run the msiexec command to uninstall the software.
We usually manually uninstall the software by going to the "Add/Delete" program in the control panel to uninstall the software, or by uninstalling the software that comes with the program.
You can run the MsiExec.exe/X {ProductCode} command to uninstall the program.
About msiexec.exe see http://technet.microsoft.com/zh-cn/library/cc759262%28v=WS.10%29.aspx
Search for ProductCode in the Registry
ProductCode is the Globally Unique Identifier (GUID) of the Windows installation package. You can obtain ProductCode through the registry.
Instance: Use msiexec.exe to automatically uninstall Xmarks.
Xmarks is a tool used to synchronize favorites. I usually use it to synchronize favorites of IE, firefox, and chrome.
First open the following location in the registry,
32-bit operating system: HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall \
Note: For a 64-bit operating system:
The 64-bit program is still in: HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall \
The 32-bit program is in: HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall \
There are many registry subkeys under Uninstall. You need to patiently search for "DisplayName" one by one to find the program's ProductCode, such.
From the registry we find the UninstallString key value: MsiExec.exe/X {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF}, then ProductCode is {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF}
You can run the MsiExec.exe/X {ProductCode} command to uninstall the program.
The unmount command should be MsiExec.exe/X {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF}
Then, directly call this command in CMD. A dialog box is displayed. Click "yes" to uninstall the software.
In automated testing, we do not want to bring up this dialog box, but want to uninstall it directly. At the same time, you do not need to add two parameters/quiet/norestart to restart the system.
The current unmount command is: MsiExec.exe/X {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF}/quiet
C # uninstall the program
C # code uninstallation is relatively simple. Of course, you can also use other languages.
Process p = new Process(); p.StartInfo.FileName = "msiexec.exe"; p.StartInfo.Arguments = "/x {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF} /quiet /norestart"; p.Start();
C # search for ProductCode in the Registry
The most troublesome thing is how to obtain ProductCode from the registry. For automated testing of non-Web programs, you often need to deal with the Registry.
Code:
Public static string GetProductCode (string displayName) {string productCode = string. empty; // if it is a 32-bit operating system (or the system is 64-bit, the program is also 64-bit) string bit32 = @ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall "; // If the operating system is 64-bit and the program is 32-Bit string bit64 = @ "SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall"; RegistryKey localMachine = Registry. localMachine; RegistryKey Uninstall = localMachine. openSubKey (bit32, true); foreach (string subkey in Uninstall. getSubKeyNames () {RegistryKey productcode = Uninstall. openSubKey (subkey); try {string displayname = productcode. getValue ("DisplayName "). toString (); if (displayname = displayName) {string uninstallString = productcode. getValue ("UninstallString "). toString (); string [] strs = uninstallString. split (new char [2] {','}); productCode = strs [1]; return productCode ;}} catch {}} return productCode ;}
Complete source code download
Click here to download the complete source code. Open it with vs2010 or later.