如何在C#中获取安装项目的自定义操作数据? [英] How can I get custom action data of a setup project in C#?

查看:72
本文介绍了如何在C#中获取安装项目的自定义操作数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有人知道如何在c#中执行该操作吗?

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

更新

在我的ProjectInstaller中:

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

我的Windows服务的 app.config

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>

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

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

推荐答案

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

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 =

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

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

You can add MessageBox and things in to aid debugging.

这篇关于如何在C#中获取安装项目的自定义操作数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆