问题
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