Editing a custom configuration section in the installer class

I am trying to update the user configuration section of the web.config file during the installation of my product in a custom action. I wanted to use the actual configuration classes for this, but when the installer starts loading my installer class, Configuration.GetSection throws a File Not Found exception as it tries to load my custom partition class from the Windows system directory. I managed to get this to work by copying the necessary assemblies into the Windows system directory, but this is not an ideal solution, since I cannot guarantee that I will always have access to this directory.

How else can I solve this problem?

My update code is as follows:

[RunInstaller(true)] public partial class ProjectInstaller : Installer { public override void Install(System.Collections.IDictionary stateSaver) { //some code here webConfig = WebConfigurationManager.OpenWebConfiguration("MyService"); MyCustomSection mySection = webconfig.GetSection("MyCustomSection") //<--File Not Found: CustomConfigSections.dll //Update config section and save config } } 

My configuration file looks like

 <configuration> <configSections> <section name="myCustomSection" type="CustomConfigSections.MyCustomSection, CustomConfigSections" /> </configSections> <myCustomSection> <!-- some config here --> </myCustomSection> </configuration> 
+4
source share
1 answer

I hope you understand the answer as it is intended.

Assuming you installed an installer to output your project. If not Right-click on the installer. Click "Add" - "Project Output" - select your project. and then you can continue to use your code.

In addition, if you use dll, with the exception of .net Ones, be sure to go there properties for copylocal = true

If you want to read the Before installation item, use the BeforeInstall Event Handler and try to read the file. ihope your problem will be solved.

If in case you want to read an item after installation, right-click on the installer project Click view-> customActions-> On Install Click Add custom action -> Select the application folder → Select Primary exit from your project and click ok.

Now click on the primary output and press F4, and in the user actions specify the entry

 /DIR="[TARGETDIR]\" 

and after that write your code as follows.

 [RunInstaller(true)] public class ProjectInstaller : Installer { public ProjectInstaller() { this.InitializeComponent(); } private void InitializeComponent() { this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall); } void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) { string path = this.Context.Parameters["DIR"] + "YourFileName.config"; // make sure you replace your filename with the filename you actually // want to read // Then You can read your config using XML to Linq Or you can use // WebConfigurationManager whilst omitting the .config from the path } 
+1
source

All Articles