- Tôi đã tạo Installhelper.cs được lấy từ Trình cài đặt.
- Ghi đè phương thức Install() bằng cách sử dụng đoạn mã này (A).
Các giá trị được chèn vào ứng dụng.config không có sẵn trong [projectName] .exe.config tệp được tạo sau khi cài đặt. Tôi đã thêm một phần như được hiển thị bên dưới trong app.config theo cách thủ công (B)Cách ghi vào app.config trong dự án thiết lập và sử dụng nó trong chương trình
dữ liệu được chuyển đến lớp trình cài đặt nhưng dữ liệu không được ghi vào các trường app.config. Chúng vẫn giống nhau trong tệp cấu hình được tạo trong khi cài đặt.
Bất kỳ Trợ giúp nào được đánh giá cao. Tôi đã dành gần một ngày cho việc này.
Mã 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;
}
}
}
Added phần trong cấu hình ứng dụng là Mã B:
<appSettings>
<add key="userName" value="" />
<add key="password" value="" />
<add key="foderPath" value="" />
</appSettings>
Cảm ơn kleopatra đã chỉnh sửa. :) – Jasmeet