2012-06-28 29 views
7
  • 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> 

Trả lời

1

Cảm ơn bạn. Chúng tôi đã có vấn đề chính xác này và không thể tìm ra lý do tại sao nó sẽ không ghi vào tệp. điều duy nhất khác tôi đã làm là lấy đường dẫn từ Ứng dụng.

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

Cảm ơn kleopatra đã chỉnh sửa. :) – Jasmeet

6

Vấn đề mã hóa này là dòng này

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

Nó không cung cấp đường dẫn nơi tệp cấu hình dựa vào quá trình cài đặt. Do đó, tệp phải được thay đổi thành

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

    Configuration config = ConfigurationManager.OpenExeConfiguration(path); 

Bây giờ nó hoạt động. :)

Các vấn đề liên quan