问题
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:
- On install with "-i" as an argument
- 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:
You will probably be better off using the "EventLogInstaller" found in the "System.Diagnostics" 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
回答2:
I can't remember or access the details right now but somewheren in that (horrible) UI for setup-projects there should be alist of 'standard' actions for, amongst others, creating an EventLog. That would be the safest way.
But you should be OK testing this w/o UAC. If it works, it works. A setup.exe runs as Admin
来源:https://stackoverflow.com/questions/3176502/will-this-code-create-an-eventlog-at-installation-time