【问题标题】:How to Write to app.config in setup project and use it in the program如何在安装项目中写入 app.config 并在程序中使用它
【发布时间】:2012-06-29 13:31:28
【问题描述】:
  • 我创建了一个从安装程序继承的 Installhelper.cs。
  • 使用这段代码 (A) 覆盖 Install() 方法。

插入到 app.config 的这些值在安装后创建的 [projectName].exe.config 文件中不可用。 我已经在 app.config 中手动添加了如下所示的部分(B)

数据被传递到安装程序类,但数据不会写入 app.config 字段。在安装过程中,它们在创建的配置文件中保持不变。

非常感谢任何帮助。我花了将近一天的时间。

代码 A:

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

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

        try
        {
            // In order to get the value from the textBox named 'EDITA1' I needed to add the line:
            // '/PathValue = [EDITA1]' to the CustomActionData property of the CustomAction We added. 

            string userName = Context.Parameters["userName"];
            string password = Context.Parameters["password"];
            string folderPath = Context.Parameters["path"];

            MessageBox.Show(userName);
            MessageBox.Show(password);
            MessageBox.Show(folderPath);

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
           //config.AppSettings.Settings.Add("userName", userName);
           //config.AppSettings.Settings.Add("password", password);
           //config.AppSettings.Settings.Add("foderPath", folderPath);





            config.AppSettings.Settings["userName"].Value = userName;
            config.AppSettings.Settings["password"].Value = password;
            config.AppSettings.Settings["foderPath"].Value = folderPath;
            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("appSettings");




        }
        catch (FormatException e)
        {
            string s = e.Message;
            throw e;
        }
    }
}

应用配置中添加的部分是 代码 B:

<appSettings>
<add key="userName" value="" />
<add key="password" value="" />
<add key="foderPath" value="" />
</appSettings>

【问题讨论】:

    标签: c#-4.0 installation app-config visual-studio-setup-proje


    【解决方案1】:

    这个编码的问题是这一行

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    

    它没有给出配置文件在安装过程中所依赖的路径。所以它必须更改为

    string path = Path.Combine(new DirectoryInfo(Context.Parameters["assemblypath"].ToString()).Parent.FullName, "[project name].exe")
    
        Configuration config = ConfigurationManager.OpenExeConfiguration(path);
    

    现在它的工作。 :)

    【讨论】:

      【解决方案2】:

      谢谢。我们遇到了这个确切的问题,无法弄清楚为什么它不会写入文件。 我做的唯一不同的事情是从应用程序中获取路径。

      string path = Application.ExecutablePath;
      Configuration config = ConfigurationManager.OpenExeConfiguration(path);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-12
        • 2012-02-20
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        相关资源
        最近更新 更多