【问题标题】:How can I get custom action data of a setup project in C#?如何在 C# 中获取安装项目的自定义操作数据?
【发布时间】:2020-01-28 13:20:15
【问题描述】:

我为我的服务创建了一个 Windows 服务和一个安装项目。 在我的 Windows 服务中,我可以让我的客户行为数据认为:

Context.Parameters["dbname"]

但我想在我的服务中访问我的客户操作数据的价值,以便在我的项目中使用它。

有人知道如何在 c# 中做到这一点吗?

更新

在我的 ProjectInstaller 中:

public ProjectInstaller()
{
    InitializeComponent();
}

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);
    string dbName = Context.Parameters["dbname"];
    AppHelper.Save(dbName+" "+ Context.Parameters["targetdir"].ToString());

    string xml = Context.Parameters["targetdir"].ToString() + "App.config";

    XmlDocument document = new XmlDocument();
    document.Load(xml);

    XPathNavigator navigator = document.CreateNavigator();
    XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);

    foreach (XPathNavigator nav in navigator.Select(@"/configuration.appSettings/add[@key='dbName']"))
    {
        nav.SetValue(dbName);
    }

    document.Save(xml);
}

我的windows服务的app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="dbName" value="totodb"/>
  </appSettings>

  <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

当我尝试安装我的项目时出现错误,提示我的 app.config 不存在。

【问题讨论】:

  • 我认为您可以将所需的内容写入应用程序配置并从服务中获取它们。例如stackoverflow.com/questions/9208442/…
  • @NDJ 我看到了示例,但是如何在我的 ProjectInstaller 中定位我的 app.config 文件?
  • @NDJ 我看到了示例,但是如何在我的 ProjectInstaller 中定位我的 app.config 文件?
  • 这不正是示例在做什么吗? Context.Parameters["TARGETDIR"].ToString() + "UpdateReportService.exe.config";
  • @NDJ 问题是我的 app.config 不在安装路径中

标签: c# windows-services setup-project custom-action


【解决方案1】:

这对我有用。您需要将 ConsoleApp2 替换为您的输出文件名。

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);

    string dbName = Context.Parameters["dbname"].ToString();
    string xml = Context.Parameters["name"].ToString() + "ConsoleApp2.exe.config";

    XmlDocument document = new XmlDocument();
    document.Load(xml);

    XmlNode dbNameNode = document.SelectSingleNode("//configuration/appSettings/add[@key='dbName']");

    dbNameNode.Attributes["value"].Value = dbName;

    document.Save(xml);
}

我的自定义操作数据是:

/name="[TARGETDIR]\" /dbname=[EDITA1]

您可以添加 MessageBox 和其他东西以帮助调试。

【讨论】:

  • 谢谢你的最好!我有我的程序停止,因为我有这个错误: System.Configuration.ConfigurationErrorsException:'配置系统初始化失败' ConfigurationErrorsException:每个配置文件只允许一个 元素,如果存在必须是根 的第一个子元素元素。 (C:\t-win\WindowsService1.exe.Config 第 6 行)
  • 在您的 app.config 中,您需要将 元素移到 元素下方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2011-09-06
相关资源
最近更新 更多