自定义操作不起作用 - Visual Studio 安装项目

发布于 2024-11-13 11:17:09 字数 2827 浏览 4 评论 0原文

过去,我们使用高级安装程序为特定项目构建 .msi 安装程序。我们的高级安装程序年度许可证已过期,因此为了避免续订费用,并且因为我认为使用 Visual Studio 也可以完成同样的任务,所以我尝试使用 Visual Studio 2010 安装项目来构建我的 .msi。

在大多数情况下,我使用 Visual Studio 构建的安装程序运行良好。然而,我们需要安装程序做的一件事是运行几个 .reg 文件,将大量设置添加到注册表中(可能值得注意的是,这是一个旧软件,仅在维护和更新,直到在不久的将来被完全替换,改变我们存储设置的方法是不切实际的)。借助高级安装程序,我们能够将 .cmd 文件作为“安装”自定义操作来执行,该操作将运行安装中也包含的这些 .reg 文件。 VS 安装项目有自定义操作,但似乎在这里它们需要是 .dll.exe 文件,因此我必须找到使用 .dll 文件的替代方法>.bat 或 .cmd 文件。

首先,我尝试将命令行项目添加到我的解决方案中,该项目仅包含 main() 方法中的以下几行:

using (Process registryInput = Process.Start("regedit.exe", "/s Settings1.reg"))
{
    registryInput.WaitForExit();
}

using (Process registryInput= Process.Start("regedit.exe", "/s Settings2.reg"))
{
    registryInput.WaitForExit();
} 

我将此项目的主要输出添加到“自定义操作”编辑器的“安装”文件夹中。尝试运行安装程序,但命令行进程似乎从未运行,并且未安装任何注册表设置。如果我从安装它的应用程序目录手动运行命令行可执行文件,它会按预期添加注册表项 - 所以问题不在于我用来调用 .reg 文件的代码。

我转向 MSDN 并将我的解决方案更改为以他们的 自定义操作演练。我创建了一个类库项目(并删除了我的命令行项目)并添加了一个安装程序类。我没有像示例中那样在 Commit() 方法中使用 Microsoft 网站 URL 启动浏览器,而是将上面的代码添加到 Install() 方法中。这就是我最终得到的结果:

[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
    public Installer1()
    {
        InitializeComponent();
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);

        using (Process registryInput = Process.Start("regedit.exe", "/s Settings1.reg"))
        {
            registryInput.WaitForExit();
        }

        using (Process registryInput = Process.Start("regedit.exe", "/s Settings2.reg"))
        {
            registryInput.WaitForExit();
        } 
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Rollback(IDictionary savedState)
    {
        base.Rollback(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Uninstall(IDictionary savedState)
    {
        base.Uninstall(savedState);
    }
}

我将这个新类库项目的主要输出添加到“自定义操作”编辑器的“安装”文件夹中。不过,当我运行安装程序时,代码似乎没有被执行,并且我的注册表设置也没有添加。我已尝试将此安装程序设置为“为所有用户安装”和“仅此用户”。

任何帮助使此自定义操作正常工作或使 .reg 文件在安装时运行的替代方法的帮助将不胜感激。先感谢您。

In the past we have used Advanced Installer to build our .msi installers for a particular project. Our yearly license for Advanced Installer has expired, so to avoid the renewal cost, and because I think the same can be accomplished with Visual Studio, I am attempting to use a Visual Studio 2010 Setup Project to build my .msi.

For the most part, the installer I have built with Visual Studio works fine. However, one thing we need the installer to do is run a couple of .reg files to add a large collection of settings to the registry (It may be worth noting that this is old software that is only being maintained and updated until it is replaced entirely in the near future. It is not practical to change our method of storing settings). With Advanced Installer, we were able to execute a .cmd file as an "Install" Custom Action that would run these .reg files that were also included in the installation. VS Setup Projects have Custom Actions, but it appears that here they are required to be either .dll or .exe files, so I must find an alternative to using a .bat or .cmd file.

First, I tried adding a Command Line project to my solution that consisted only of the following lines in the main() method:

using (Process registryInput = Process.Start("regedit.exe", "/s Settings1.reg"))
{
    registryInput.WaitForExit();
}

using (Process registryInput= Process.Start("regedit.exe", "/s Settings2.reg"))
{
    registryInput.WaitForExit();
} 

I added the Primary Output of this project to the "Install" folder of the "Custom Actions" editor. Tried to run the installer, but the command line process never seemed to run and no registry settings were installed. If I manually ran the command line executable from the application directory where it was installed, it added the registry entries as intended - so the problem is not with the code I'm using to call the .reg files.

I turned to MSDN and changed my solution to be modeled after their Custom Actions Walkthrough. I created a Class Library project (and removed my Command Line project) and added an Installer Class. Instead of starting up a browser using Microsoft's website URL in the Commit() method as shown in their example, I added the code above to the Install() method. Here is what I ended up with:

[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
    public Installer1()
    {
        InitializeComponent();
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);

        using (Process registryInput = Process.Start("regedit.exe", "/s Settings1.reg"))
        {
            registryInput.WaitForExit();
        }

        using (Process registryInput = Process.Start("regedit.exe", "/s Settings2.reg"))
        {
            registryInput.WaitForExit();
        } 
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Rollback(IDictionary savedState)
    {
        base.Rollback(savedState);
    }

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Uninstall(IDictionary savedState)
    {
        base.Uninstall(savedState);
    }
}

I added the Primary Output of this new Class Library project to the "Install" folder of the "Custom Actions" editor. Still, when I run the installer, the code does not appear to be executed and my registry settings are not added. I have tried this installer both set to "Install for all users" and "This user only".

Any help to either get this Custom Action working or an alternative method to get a .reg file to run on install will be greatly appreciated. Thank you in advance.

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

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

发布评论

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

评论(4

你是暖光i 2024-11-20 11:17:09

我刚刚遇到了同样的问题,回复:安装程序未选择自定义操作。解决方案是以管理员身份运行 Visual Studio。

即使我是我的计算机的完全管理员,没有任何限制(AFAIK),安装程序永远不会选择自定义操作。一旦我关闭 Visual Studio,然后以管理员身份重新启动(右键单击 > 以管理员身份运行),安装程序就会立即选择自定义操作。

I just ran across this same issue, re: the custom action not being picked up by the installer. The resolution was to run Visual Studio as an administrator.

Even though I'm a full admin on my machine without any restrictions (AFAIK), the installer would never pick up the custom actions. As soon as I closed down Visual Studio and then restarted as an administrator (right click > run as administrator), the custom actions were immediately picked up by the installer.

海之角 2024-11-20 11:17:09

我在这个问题上敲了敲键盘——只有在将我的自定义安装操作放入安装程序类的构造函数中后,它才运行。

我按照在这里找到的教程进行操作: http:// msdn.microsoft.com/en-us/library/d9k65z2d(v=VS.100).aspx

I banged my head on the keyboard for a bit on this one - and only after putting my custom installation actions in the Constructor of the Installer Class made it run.

I followed the tutorial I found here: http://msdn.microsoft.com/en-us/library/d9k65z2d(v=VS.100).aspx

不打扰别人 2024-11-20 11:17:09

看看这个链接是否有帮助,它帮助了我:

Visual Studio 2008 安装程序,自定义操作。断点不触发

基本上,代码在

base.Install(stateSaver);

之后没有被处决。所以把base.Install(stateSaver);作为方法中的最后一行。

See if this link helps, it helped me:

Visual Studio 2008 Installer, Custom Action. Breakpoint not firing

Basically, code after the

base.Install(stateSaver);

is not getting executed. So put the base.Install(stateSaver); as the last line in the method.

财迷小姐 2024-11-20 11:17:09

这看起来似乎很明显,但它让我困惑了一段时间,所以不妨将其发布。

我只是右键单击安装程序项目,然后单击“安装”和“卸载”。但是,更改代码后必须重建Installer项目! (也可能是带有安装程序类的项目)

This may seem obvious but it caught me out for a while, so might as well post it.

I was just right-clicking on the installer project and then "Install" and "Uninstall". However, you have to rebuild the Installer project after changing the code! (and probably the project with the installer class as well)

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