此代码会在安装时创建事件日志吗?

发布于 2024-09-08 04:09:26 字数 1347 浏览 3 评论 0原文

遵循Henk的建议< /a>,我在 VS10 中创建了一个安装项目,目的是添加自定义操作。此自定义操作有望在以管理员身份运行时(即安装期间)添加事件日志,而不是让我的应用程序在具有 UAC 的操作系统上抛出异常。

不幸的是,我通常无法访问使用 UAC 的操作系统。下次再做,希望安装顺利。

考虑到这一点,下面的代码中有什么明显错误的地方吗?

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;
            }

        }
    }
}

该项目的输出被吸入安装项目中。然后,我有两个自定义操作:

  1. 在安装时使用“-i”作为参数
  2. 在卸载时使用“-u”作为参数

我并不期待免费的代码审查,但我正在冒险进入未知的领域,所以我'如果我放错了垃圾,请注意。

PS 我特别担心我指定的是实际的日志名称,而不是实际的源。这会重要吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-09-15 04:09:26

使用“System.Diagnostics”程序集中的“EventLogInstaller”可能会更好。

当您创建自定义组件,然后将事件日志组件添加到设计图面,填写该组件的属性,然后单击属性窗口中的“添加安装程序”链接/命令时,您可以看到此功能的实现。这将添加一个项目安装程序组件,其中将包含一个事件日志安装程序组件。

事件日志安装程序组件正是您所寻找的,基本上它是一个 Windows 安装程序操作,可以在您创建 Windows 安装程序包 (MSI) 时运行。您所要做的就是在 Visual Studio 部署项目的“自定义操作编辑器”中指定安装程序操作。 MSDN 库中有大量有关自定义操作的信息。

另请查看以下内容:

EventLogInstaller类

安装程序工具 (Installutil.exe) - msdn.microsoft.com/en-us/library/50614e95(VS.80).aspx

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

白衬杉格子梦 2024-09-15 04:09:26

我现在不记得或无法访问详细信息,但在设置项目的(可怕的)用户界面中的某个地方应该有一个“标准”操作列表,用于创建事件日志等。这将是最安全的方法。

但你应该可以在没有 UAC 的情况下测试这个。如果有效,那就有效。 setup.exe 以管理员身份运行

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文