ASpeak IOpine!

Uninstallation from C#

Posted on March 17, 2009

I wanted to programatically uninstall an application installed in my system. Following code is a working piece to uninstall any custom application from the system.

Note: Following code snippet is not the best example of good coding standards ! It was just a sample I was trying to use to achieve something. The code is a mixture of coding styles lifted across various sites. Please excuse for that :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
private static int Uninstall()
{
        Microsoft.Win32.RegistryKey Fregistry =
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE")
        .OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion")
        .OpenSubKey("Installer").OpenSubKey("UserData")
        .OpenSubKey("S-1-5-18").OpenSubKey("Products");
        string[] Names = Fregistry.GetSubKeyNames();
        string uninstall = "";
        string ApplicationName = "My Custom Application";
        for (int i = 0; i < Names.Length; i++)
        {
            Microsoft.Win32.RegistryKey FTemp = Fregistry.OpenSubKey(Names[i]).OpenSubKey("InstallProperties");
            if (FTemp == null)
                continue;
            string appFound = (FTemp.GetValue("DisplayName") == null) ? String.Empty : FTemp.GetValue("DisplayName").ToString();
            if (!String.IsNullOrEmpty(appFound))
                System.Diagnostics.Trace.WriteLine("Found Software: " + appFound);
            if (String.Compare(appFound, ApplicationName, true) == 0)
            {
                String installedVersion = FTemp.GetValue("DisplayVersion").ToString();
                if (!installedVersion.StartsWith("3.1."))//can be removed if only one version is installed
                    continue;
                object obj = FTemp.GetValue("UninstallString");
                if (obj == null)
                    uninstall = "";
                else
                    uninstall = obj.ToString();
                i = Names.Length;
            }
        }
        if (String.IsNullOrEmpty(uninstall))
        {
            return -1;
        }
        System.Console.WriteLine(uninstall);
        System.Diagnostics.Process FProcess = new System.Diagnostics.Process();
        string temp = "/passive /x{" + uninstall.Split("/".ToCharArray())[1].Split("I{".ToCharArray())[2];
        FProcess.StartInfo.FileName = uninstall.Split("/".ToCharArray())[0];
        FProcess.StartInfo.Arguments = temp;
        FProcess.StartInfo.UseShellExecute = false;
        FProcess.Start();
        FProcess.WaitForExit();
        if (FProcess.ExitCode != 0)
            return -1;
        else
        {
            return 0;
        }
}
Tagged as: , No Comments

Generate Random GUID by script

Posted on August 13, 2008

I had a task in hand to read and update a few registry settings. Since the registry keys might change in future and some more changes might be required on the fly, I decided to go with script instead of a config driven executable. Also, one compelling reason was that it has been some time since I did some good amount of scripting so I wanted to brush my scripting skills again.

Initially I wanted to use Windows Powershell but I couldn't proceed far with it as the scripts require special privilege and security certificates to run. I don't want to trouble my client with these hassles and ask him to install PowerShell. I am still exploring easy ways to allow a PS script to run without previliges and certificate/signing frustrations. Finally I settled with JScript which I have used extensively for my own purpose.

One of the requirement was to generate unique GUIDs. I looked around for generating unique GUID values using JScript or VBScript but couldn't find something useful. I am not looking for re-writing a logic to generate a GUID with a specified pattern with all the scemantics. I just want a simple object which exposes the functionality to generate a new unique GUID and the object must be natively supported so that my client won't require to install something additionally.

As usual after exploring all the way I stumbled across Microsoft Scripting Guy and the solution was a piece of cake ! Following is the answer

var typeLib = new ActiveXObject("Scriptlet.TypeLib");
var newGuid = typeLib.Guid;

Isn't it easy ?

Power of Unix on Windows

Posted on July 22, 2008

I am an ardent admirer of console commands. For a very fast execution of the most simple tasks, I rely on cmd. About an year back I saw my tech architect using Grep on windows. Until then I didn't know how effective it could be on Windows platform. At that very moment I downloaded unix-utilities for windows. I have realized its power many times in my profession. Very often it is required to find a few pieces of information from a pile of XML logs occupying around 20 Megs on my system. I wonder if I could have done it if I were not having the power of unix on windows. A variety of utilities are available for most of the tasks. Undoubtedly, my favourite is grep because of its splendid blend of simplicity and power.

I like powershell too, but for simple tasks it seems like an overkill. I am not an expert of powershell but for simple task of finding some information recursively, I think grep is great. The command is simple and execution is very fast.

With the evolution of powershell, it seems micrososft is empowering users with powerful console commands. WMI is good but still it was not a general purpose scripting platform. Powershell leverages both its potential for managing windows and perform quick one line commands. I like a few of the commandlets available in powershell like "ps" is a quick way to dump all the available processes with relevant information. Powershell becomes really fun when one has the PowerShellPlus which is an interactive scripting environment for powershell which also has intellisense support.

If you do not like default appearance and behaviour of Command prompt, which has not changed much since the DOS era, then go for Console. It is an excellent piece of open source creativity which gives better control on command prompt as Linux users have always enjoyed. It allows multiple tabbed console prompts, coloured background with custom picture and true alpha blending.

Please share your experiences and suggestions.