How can I get custom action data of a setup project in C#?

强颜欢笑 提交于 2020-03-05 04:05:08

问题


I create a windows service and a setup project for my service. In my windows service I can get my customer action data thinks to this :

Context.Parameters["dbname"]

But I want to access to the value of my customer action data in my service to use it in my project.

Someone have any idea how to do it in c# ?

UPDATE

In my 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);
}

the app.config of my windows service :

<?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>

When I try to install my project I have an error which say that my app.config doesn't exist.


回答1:


This is working for me. You will need to replace ConsoleApp2 with your output filename.

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

and my custom action data is:

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

You can add MessageBox and things in to aid debugging.



来源:https://stackoverflow.com/questions/59949715/how-can-i-get-custom-action-data-of-a-setup-project-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!