您可以在OnBeforeInstall方法中编辑service.exe.config吗?

时间:2019-12-10 05:43:06

标签: c# .net windows-services

所以我要完成的工作是在安装时将参数传递给Windows服务,然后使用这些参数编辑exe.config文件。我一直关注tutorial here,并且能够在Service.cs文件中的OnStart方法期间读取/写入配置文件。

当我尝试在ProjectInstaller.cs文件的OnBeforeInstall方法中执行相同操作时,未进行这些更改。安装或启动服务后,我是否只能更改配置文件?

这是我到目前为止所拥有的:

namespace MyNewService
{
RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{

    public ProjectInstaller(string[] args)
    {
        InitializeComponent();
    }

    protected override void OnBeforeInstall(IDictionary savedState)
    {
        string parameter = "MySource1\" \"MyLogFile1";
        Context.Parameters["assemblypath"] = "\"" + Context.Parameters["assemblypath"] + "\" \"" + parameter + "\"";
        base.OnBeforeInstall(savedState);

        string myProp = GetContextParameter("myProp").Trim();
        if (!string.IsNullOrEmpty(myProp))
        {
            AddUpdateAppSettings("testProp", myProp);
        }
        ReadAllSettings();


    }

    public string GetContextParameter(string key)
    {
        string sValue = "";
        try
        {
            sValue = this.Context.Parameters[key].ToString();
        }
        catch
        {
            sValue = "";
        }
        return sValue;
    }

    public void WriteToSettings(string key, string value)
    {
        try
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;
            if (settings[key] == null)
            {
                settings.Add(key, value);
            }
            configFile.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
        }
        catch (ConfigurationErrorsException)
        {

        }
    }

    public void AddUpdateAppSettings(string key, string value)
    {
        try
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;
            if (settings[key] == null)
            {
                settings.Add(key, value);
            }
            else
            {
                settings[key].Value = value;
            }
            configFile.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
        }
        catch (ConfigurationErrorsException)
        {
        }
    }

    public string ReadSetting(string key)
    {
        string result = "";
        try
        {
            var appSettings = ConfigurationManager.AppSettings;
            result = appSettings[key] ?? "Not Found";
            eventLog1.WriteEntry("This is the result of reading the appSettings: " + result);
        }
        catch (ConfigurationErrorsException)
        {
            Console.WriteLine("Error reading app settings");
        }
        return result;
    }

    public void ReadAllSettings()
    {
        try
        {
            var appSettings = ConfigurationManager.AppSettings;

            if (appSettings.Count == 0)
            {
                eventLog1.WriteEntry("AppSettings is empty.");
            }
            else
            {
                foreach (var key in appSettings.AllKeys)
                {
                    eventLog1.WriteEntry("Key: " + key + " Value: " + appSettings[key]);
                }
            }
        }
        catch (ConfigurationErrorsException)
        {
            eventLog1.WriteEntry("Error reading app settings");
        }
    }
}

我误解了在服务安装过程中如何以及何时访问配置文件?还是我的Windows服务从持久值读取的更简单方法?

0 个答案:

没有答案
相关问题