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

MSDN code examples could be incorrect as well

Posted on September 8, 2008

It was really odd for me to find an error in MSDN code example which took almost a couple of hours to figure out what is wrong. I am working in VSTO (Visual Studio Tool for Office). I am developing a plugin which is suppose to override some of the specific Office 2007 functionalities and implement some custom actions. My quest for knowledge took me here. I found this page very interesting and I found the explanation for exactly what was required. I took the C# code example as it is and tried to play around in my own sample. Sadly, the example which is given on the page in C# has a flaw.

public void mySave(IRibbonControl control, bool cancelDefault)
{
    If (repurposing)
    {
        MessageBox.Show("The Save button has been temporarily repurposed.");
        cancelDefault = False;
    }
    else
    {
        cancelDefault = False;
    }
}

In this code example, the second parameter should be a reference type. Visual basic example in the same MSDN page shows it correctly but the C# example as given above is incorrect. I respect MSDN so much that I didn't care to see if anything is wrong in the code I lifted. After fiddeling around with a number of things and learning a whole lot than intended, I found the problem.
Lesson learnt! I will be more careful from next time :)

Quick script in C#/VB and validate LINQ Expressions

Posted on July 19, 2008

I have been trying to push myself for learning LINQ and Lambda expressions for quite some time now. I try to create some dummy projects sometimes but since our current project is being developed in .NET Framework v2.0 so these very new things get out of touch very soon. Also, there is so much happening around in the field of technology that keeping track of technological innovations in .NET field alone gets tough with the given schedule.

I was looking for ways to get chance to work on LINQ in the office itself. Recently I found a great utility for that purpose. It is called LINQPad.

LINQPad Logo

It is a very good utility which could be used to evaluate LINQ expressions on the fly. It can also be used for quick scripting in C# and evaluate the outcome. To do a quick analysis of some syntax I normally use Immediate Window in Visual Studio but it is only applicable for one liner simple statements.

If you need to write a snippet and evaluate it then Immediate Window will not be very helpful. LINQPad can do that job for me. I can write quick code snippets and evaluate the output very quickly. Currently it supports both C# and VB.NET. It can also give you the SQL and Lambda expressions for the LINQ queries provided.

Interestingly, you can also integrate nHibernate with LINQPad. Follow this link.

It indeed is a great utility which meets my objective very precisely. Happy LINQing!

Download LINQPad

Video Tutorials