views:

54

answers:

1

Following the advice of Henk, I've created a Setup Project in VS10 with the aim of adding a custom action. This custom action will hopefully add an EventLog whilst running as admin (ie during installation) rather than having my app throw an exception on OSes with UAC.

Unfortunately, I don't ordinarily have access to an OS that uses UAC. The next time I do, I hope the installation will go smoothly.

With that in mind, is there anything in the below code which is obviously wrong?

using System;
using System.Diagnostics;

namespace EventLogCreator
{
    class Program
    {
        static void Main(string[] args)
        {
            switch (args[0])
            {
                case "-i":
                    if (!EventLog.Exists("SSD Log"))
                    {
                        Console.WriteLine("Log not found, creating.");
                        EventLog.CreateEventSource("setup", "SSD Log");
                    }
                    break;
                case "-u":
                    if (EventLog.Exists("SSD Log"))
                    {
                        Console.WriteLine("Log found, removing.");
                        EventLog.Delete("SSD Log");
                    }
                    break;
            }

        }
    }
}

The output of this project is sucked into the setup project. I then have two custom actions:

  1. On install with "-i" as an argument
  2. On uninstall with "-u" as an argument

I'm not expecting a free code review, but I'm venturing into the unknown here, so I'd appreciate a heads up if I'm humping the wrong bit of trash.

PS I'm particularly concerned that I'm specifying the actual log name, but not an actual source. Will this matter?

+1  A: 

You will probably be better off using the "EventLogInstaller" found in the "System.Configuration.Install" assembly.

You can see a implementation of this when you create a custom component, then adding a event log component to the design surface, fill in the properties for the component, then click on the "Add Installer" link/command in property window. This will add a project installer component, which will contain a event log installer component.

The event log installer component is what you are looking for, basically it is a windows installer action that can be run when you create a windows installer package (MSI). All you have to do is specify the installer action in the "Custom Actions Editor" of your visual studio deployment project. There is quite a bit of information regarding custom actions in the MSDN library.

Also have a look at the following:

EventLogInstaller Class

Installer Tool (Installutil.exe) - msdn.microsoft.com/en-us/library/50614e95(VS.80).aspx

Schalk