问题
I have a VS solution with following structure:
Library project (.dll)
Application using the #1 library project
I have app.config defined in the application (#2) which defines a SaveLogsToDirectory
path in appSettings. This value is eventually used by the library project to save the logs generated.
Simple use of api System.Configuration.ConfigurationManager.AppSettings["SaveLogsToDirectory"]
in the library fetches the value from app.config.
Library project has a custom System.Configuration.Install.Installer class defined. When the application is uninstalled from windows via Control Panel, I would like the logs generated at path SaveLogsToDirectory to be deleted. Issue is the below code returns null only and only during the uninstall execution
System.Configuration.ConfigurationManager.AppSettings["SaveLogsToDirectory"]
One of the other approaches I tried was using System.Configuration.ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly())
but during uninstall the api Assembly.GetExecutingAssembly()
returns reference to library project.
I need help on how I can access the application assembly from library during uninstall? One thing to mention is that I cannot provide a class path defined in application to OpenExeConfiguration api since the dll can be used by any other application and that other application may not have that class defined.
回答1:
As an option you can store the dll settings in config file of the dll rather than config file of the application.
Then you can easily use OpenExeConfiguration
and pass the dll address as parameter and read the settings.
To make it easier and harmonic to reading from app settings, you can create a like following and use it this way: LibrarySettings.AppSettings["something"]
. Here is the a simple implementation:
using System.Collections.Specialized;
using System.Configuration;
using System.Reflection;
public class LibrarySettings
{
private static NameValueCollection appSettings;
public static NameValueCollection AppSettings
{
get
{
if (appSettings == null)
{
appSettings = new NameValueCollection();
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var config = ConfigurationManager.OpenExeConfiguration(assemblyLocation);
foreach (var key in config.AppSettings.Settings.AllKeys)
appSettings.Add(key, config.AppSettings.Settings[key].Value);
}
return appSettings;
}
}
}
Notes: Just in case you don't want to rely on Assembly.ExecutingAssembly
when uninstall is running, you can easily use TARGETDIR property which specifies the installation directory. It's enough to set CustomActionData
property of the custom action to /path="[TARGETDIR]\"
, then inside the installer class, you can easily get it using Context.Parameters["path"]
. Then in the other hand you know the name of the dll file and using OpenMappedExeConfiguration
by passing config file address as parameter, read the settings.
To setup a custom installer action and getting target directory, you may find this step-by-step answer useful:Visual Studio Setup Project - Remove files created at runtime when uninstall.
来源:https://stackoverflow.com/questions/57316014/get-access-to-appsettings-from-custom-installer-during-uninstall-beforeuninstal