这些插入到 app.config 的值在 中不可用。 [项目名称].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>
最佳答案
这种编码的问题是这一行
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);
现在它的工作。 :)
关于c#-4.0 - 如何在setup工程中写入app.config并在程序中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11237702/