在卸载过程中从自定义安装程序访问AppSettings(BeforeUninstall)

编程入门 行业动态 更新时间:2024-10-27 12:42:05
本文介绍了在卸载过程中从自定义安装程序访问AppSettings(BeforeUninstall)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个具有以下结构的VS解决方案:

I have a VS solution with following structure:

  • 库项目(.dll)

  • Library project (.dll)

    使用#1库项目的应用程序

    Application using the #1 library project

    我在应用程序(#2)中定义了app.config,该应用程序在appSettings中定义了 SaveLogsToDirectory 路径.库项目最终使用此值来保存生成的日志.

    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.

    api的简单使用 System.Configuration.ConfigurationManager.AppSettings ["SaveLogsToDirectory"] 库中的内容将从app.config中获取值.

    Simple use of api System.Configuration.ConfigurationManager.AppSettings["SaveLogsToDirectory"] in the library fetches the value from app.config.

    库项目有一个自定义的 System.Configuration.Install.Installer 类.当通过控制面板"从Windows卸载应用程序时,我希望删除在路径 SaveLogsToDirectory 中生成的日志.问题是下面的代码仅在卸载执行期间返回null,并且仅在卸载执行期间返回

    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"]

    我尝试过的其他方法之一是使用 System.Configuration.ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly())

    One of the other approaches I tried was using System.Configuration.ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly())

    但是在卸载过程中,api Assembly.GetExecutingAssembly()返回对库项目的引用.

    but during uninstall the api Assembly.GetExecutingAssembly() returns reference to library project.

    在卸载过程中如何从库访问应用程序集时需要帮助吗?值得一提的是,我无法向OpenExeConfiguration api提供应用程序中定义的类路径,因为该dll可以被任何其他应用程序使用,并且其他应用程序可能未定义该类.

    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.

    推荐答案

    作为一种选择,您可以将dll设置存储在dll的配置文件中,而不是应用程序的配置文件中.

    As an option you can store the dll settings in config file of the dll rather than config file of the application.

    然后,您可以轻松地使用 OpenExeConfiguration 并将dll地址作为参数传递并读取设置.

    Then you can easily use OpenExeConfiguration and pass the dll address as parameter and read the settings.

    要使其更容易和和谐地读取应用程序设置,您可以创建一个类似以下内容并以这种方式使用它: LibrarySettings.AppSettings ["something"] .这是一个简单的实现:

    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; } } }

    注意事项:以防万一您在卸载运行时不想依赖 Assembly.ExecutingAssembly 的情况,可以轻松使用 TARGETDIR 属性,用于指定安装目录.只需将自定义操作的 CustomActionData 属性设置为/path ="[TARGETDIR] \" ,然后在安装程序类中,就可以轻松地使用Context.Parameters ["path"] .然后,另一方面,您知道dll文件的名称,并通过将配置文件地址作为参数使用 OpenMappedExeConfiguration 来读取设置.

    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.

    要设置自定义安装程序操作并获取目标目录,您可能会发现此分步解答很有用: Visual Studio安装项目-卸载时删除在运行时创建的文件.

    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.

  • 更多推荐

    在卸载过程中从自定义安装程序访问AppSettings(BeforeUninstall)

    本文发布于:2023-11-14 21:36:41,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1588559.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:自定义   安装程序   过程中   BeforeUninstall   AppSettings

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!