【发布时间】:2016-05-23 23:46:05
【问题描述】:
我创建了一个 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 文件。
注意:截图后,我也尝试将屏幕右上角的Access Modifier从Internal更改为Public,结果还是一样。
生成的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。
我已经试过了。但这不会更新 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 文件。
【问题讨论】:
-
您在配置文件中放置了哪些需要在安装时更改的内容?
-
服务监控特定文件夹并处理其中的文件。我希望用户能够在安装过程中进行设置
标签: c# .net windows-installer installation