2012-06-28 19 views
7
  • 我创建了一个Installhelper.cs,它从安装程序中创建。
  • 使用这段代码(A)覆盖Install()方法。

被插入到在app.config这些值不是在被安装后创建的[项目] .exe.config文件与可用。 我已经在app.config中手动添加了如下部分(B)如何在安装项目中写入app.config并在程序中使用它

数据被传递给安装程序类,但数据未写入到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> 

回答

1

谢谢。我们有这个确切的问题,并不知道为什么它不会写入文件。 我做的唯一不同的事情是从应用程序获取路径。

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

感谢kleopatra的编辑。 :) – Jasmeet

6

这种编码的问题是这条线

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

现在它的工作。 :)

相关问题