c# - 使用 Windows Installer 安装时如何更改应用程序的 exe.config 文件

标签 c# .net windows-installer installation

我已经创建了一个 Windows 服务应用程序。

我还为服务应用程序创建了一个 Windows 安装程序。

在安装程序项目中,我添加了一个自定义屏幕,向用户显示一个输入值的屏幕(右键单击设置项目 -> 查看 -> 用户界面 -> 右键单击​​“开始”部分 -> 添加对话框 ->文本框 -> 确定)

我已将该属性添加为自定义操作的参数。

我已将代码添加到:

private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)

并且能够将使用安装过程输入的自定义值写入文本文件。

private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
    string path = @"c:\test\123.txt";

    if (!File.Exists(path))
    {
        File.Create(path).Dispose();
        using (TextWriter tw = new StreamWriter(path))
        {
            tw.WriteLine("The setting is: " + Context.Parameters["PathValue"]);
            tw.Close();
        }

    }

    else if (File.Exists(path))
    {
        using (TextWriter tw = new StreamWriter(path))
        {
            tw.WriteLine("The setting is: " + Context.Parameters["PathValue"]);
            tw.Close();
        }
    }
}

现在,我只想将值写入应用程序的 applicationame.exe.config 文件。

我已经使用“项目属性”窗口为服务创建了一个配置文件:enter image description here

注意:截屏后,我也尝试更改Access Modifier在屏幕右上角来自 InternalPublic结果还是一样。

生成的applicationname.exe.config文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WindowsService2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <applicationSettings>
        <WindowsService2.Properties.Settings>
            <setting name="setting1" serializeAs="String">
                <value>default111</value>
            </setting>
        </WindowsService2.Properties.Settings>
    </applicationSettings>
</configuration>

如何在不手动解析 XML 的情况下写入此内容?

请注意,我的 applicationname.exe.config 文件不是格式:<add key="setting1" value="default111" />因为某些原因。

此外,我的 bin 文件夹中没有 app.config 文件。我只有 applicationname.exe.config。

enter image description here

我已经试过了。但这不会更新 application.exe.config 文件,也不会引发任何错误。

private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{

    Configuration config = ConfigurationManager.OpenExeConfiguration(@System.Reflection.Assembly.GetExecutingAssembly().Location);
    string value = System.Configuration.ConfigurationManager.AppSettings["setting1"];
    System.Configuration.ConfigurationManager.AppSettings["setting1"] = Context.Parameters["PathValue"];
    config.Save(ConfigurationSaveMode.Modified);

}

我也尝试了以下方法但没有用(虽然没有错误消息):

System.Configuration.ConfigurationManager.AppSettings.Set("setting1", Context.Parameters["PathValue"]);

请帮助我弄清楚如何在不解析 XML 文件的情况下将值写入 applicationame.exe.config 文件。

最佳答案

总体问题是您没有在应用程序环境中运行。您的代码是通过反射安装的程序集从 msiexec.exe 调用的,并且可能在 SYSTEM 帐户下运行。这就是为什么它不只是工作。

另一个可能的问题是您的 Bin 文件夹屏幕截图无关紧要。 Bin 文件夹与文件的安装位置(很可能在 Program Files 中)之间没有联系,并且没有证据表明您在安装中包含了配置文件。没有“Bin 文件夹中的所有内容都安装到系统中”的规则。因此,您的目标应用程序文件夹的屏幕截图对于查看配置文件是否存在更有用 - 您的自定义操作将在所有文件安装后运行,因此请查看那里。

也不清楚(至少对我而言)GetExecutionAssembly().Location 会返回正确的值。在调用托管代码自定义操作时使用反射加载和实例化程序集,并且位置文档暗示 LoadByte 程序集(可能在加载代码中使用)导致空字符串。因此可能值得尝试不同的方法,例如 GetExecutingAssembly().GetName().CodeBase。

此外,添加路径作为安装程序类的参数需要非常小心,并且您的帖子没有显示您所做的。如果您的属性名称是 MYPATH(并且它必须是大写的),那么您的参数需要是/PathValue="[MYPROP]\"并带有结尾的反斜杠。有一堆参数传递给安装程序类方法(包括您的程序集名称),反斜杠用于阻止整个参数列表变得不连贯。您不会在其他自定义操作中遇到此问题,因为您的参数是唯一传递的参数。

关于c# - 使用 Windows Installer 安装时如何更改应用程序的 exe.config 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402271/

相关文章:

c# - 获取 "Could not create SSL/TLS secure channel"

c# - 如何使鼠标卡住 c#

c# - InstallShield 内部错误 2769 - 安装过程中出现错误 1001

.net - 如何将 .NET Framework 4.0 Client Profile 附加到我的安装项目?

java - 我正在尝试从 Android 设备获取光传感器数据以在 Unity 应用程序中使用

c# - 如何通过 javascript 添加 ASP DropDown 值?

c# - 从 Silverlight 中的文件夹加载资源 ".resx"

c# - 如何设计一个Bold Label控件?

.net - 应该在多大程度上使用反射?

properties - 无法将属性传递给 WiX 自定义操作