C# 在卸载期间(卸载前)从自定义安装程序访问AppSettings
我有一个VS解决方案,其结构如下:C# 在卸载期间(卸载前)从自定义安装程序访问AppSettings,c#,.net,winforms,setup-deployment,appsettings,C#,.net,Winforms,Setup Deployment,Appsettings,我有一个VS解决方案,其结构如下: 库项目(.dll) 使用#1库项目的应用程序 我在应用程序(#2)中定义了app.config,它在appSettings中定义了一个SaveLogsToDirectory路径。库项目最终使用此值保存生成的日志 简单使用apiSystem.Configuration.ConfigurationManager.AppSettings[“SaveLogsToDirectory”] 在库中,从app.config获取值 库项目定义了自定义System.Configu
SaveLogsToDirectory
路径。库项目最终使用此值保存生成的日志
简单使用apiSystem.Configuration.ConfigurationManager.AppSettings[“SaveLogsToDirectory”]
在库中,从app.config获取值
库项目定义了自定义System.Configuration.Install.Installer类。当通过控制面板从windows卸载应用程序时,我希望删除在路径SaveLogsToDirectory生成的日志。问题是下面的代码仅返回null,并且仅在卸载执行期间返回null
System.Configuration.ConfigurationManager.AppSettings[“SaveLogsToDirectory”]
我尝试的另一种方法是使用System.Configuration.ConfigurationManager.OpenExeConfiguration(Assembly.getExecutionGassembly())
但是在卸载api的过程中,程序集.getExecutionGassembly()返回对库项目的引用
我需要有关如何在卸载期间从库访问应用程序集的帮助?需要提及的一点是,我无法提供在application to OpenExeConfiguration api中定义的类路径,因为该dll可由任何其他应用程序使用,而其他应用程序可能没有定义该类。作为一个选项,您可以将dll设置存储在dll的配置文件中,而不是应用程序的配置文件中 然后,您可以轻松地使用
OpenExeConfiguration
,将dll地址作为参数传递并读取设置
为了使阅读应用程序设置更容易、更和谐,您可以创建一个类似的如下内容并以这种方式使用:LibrarySettings.AppSettings[“something”]
。下面是一个简单的实现:
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;
}
}
}
注意:以防您不想依赖于程序集。在卸载运行时,您可以轻松使用指定安装目录的属性。将自定义操作的CustomActionData
属性设置为/path=“[TARGETDIR]\”
,就足够了,然后在安装程序类中,您可以使用上下文参数[“path”]
轻松获得它。另一方面,您知道dll文件的名称,并使用OpenMappedExeConfiguration
将配置文件地址作为参数传递,读取设置
要设置自定义安装程序操作并获取目标目录,您可能会发现这个分步回答很有用:。安装程序知道安装目录。问题是,它不知道配置文件名。作为一个选项,您可以将设置存储在dll的配置文件中。这样,安装程序也会知道配置文件名(yourdll.config)并可以使用它。