使用Windows应用程序修改Windows服务使用的设置

时间:2013-02-22 22:05:41

标签: c# windows service settings installshield

我目前有一个Windows服务(作为LocalSystem运行),我正在使用InstallSheild LE进行安装。此服务旨在从本地数据库文件中读取一些数据,将其打包并按设定的时间间隔将其发布到外部服务器。而是将数据库位置,服务器URL等硬编码,我想从设置文件中读取它们。我可以使用App.config轻松地做到这一点,但是从我的研究中我得到的结论是,在安装后很难/不可能修改App.config(或Program Files中的任何文件)。

我的问题是拥有一个应用程序的最佳方法是什么,我可以运行以修改服务的必要设置,而无需“以管理员身份运行”。我应该将这些设置放在注册表中吗?将它们放在AppData中是正确的答案,如果是这样,那些设置如何在更改应用程序和服务的设置之间共享?

我更像是一名Web应用程序开发人员,对桌面应用程序/服务开发还没有多少经验,所以我们非常感谢正确方向的任何一点。

1 个答案:

答案 0 :(得分:1)

您可以在应用程序安装目录之外找到App.Config,并将其放在公共文件夹(如AppData)中。然后,您将告诉您的应用程序从那里加载它,而不是从应用程序安装目录中将其拉入。

ConfigurationManager.OpenMappedExeConfig允许您从自定义位置加载配置。

// Access a configuration file using mapping. 
  // This function uses the OpenMappedExeConfiguration  
  // method to access a new configuration file.    
  // It also gets the custom ConsoleSection and  
  // sets its ConsoleEment BackgroundColor and 
  // ForegroundColor properties to green and red 
  // respectively. Then it uses these properties to 
  // set the console colors.   
  public static void MapExeConfiguration()
  {

    // Get the application configuration file.
    System.Configuration.Configuration config =
      ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);

    Console.WriteLine(config.FilePath);

    if (config == null)
    {
      Console.WriteLine(
        "The configuration file does not exist.");
      Console.WriteLine(
       "Use OpenExeConfiguration to create the file.");
    }

    // Create a new configuration file by saving  
    // the application configuration to a new file. 
    string appName = 
      Environment.GetCommandLineArgs()[0];

    string configFile =  string.Concat(appName, 
      ".2.config");
    config.SaveAs(configFile, ConfigurationSaveMode.Full);

    // Map the new configuration file.
    ExeConfigurationFileMap configFileMap = 
        new ExeConfigurationFileMap();
    configFileMap.ExeConfigFilename = configFile;

    // Get the mapped configuration file
   config = 
      ConfigurationManager.OpenMappedExeConfiguration(
        configFileMap, ConfigurationUserLevel.None);

    // Make changes to the new configuration file.  
    // This is to show that this file is the  
    // one that is used. 
    string sectionName = "consoleSection";

    ConsoleSection customSection =
      (ConsoleSection)config.GetSection(sectionName);

    if (customSection == null)
    {
        customSection = new ConsoleSection();
        config.Sections.Add(sectionName, customSection);
    }
    else 
        // Change the section configuration values.
        customSection =
            (ConsoleSection)config.GetSection(sectionName);

    customSection.ConsoleElement.BackgroundColor =
        ConsoleColor.Green;
    customSection.ConsoleElement.ForegroundColor =
        ConsoleColor.Red;

    // Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified);

    // Force a reload of the changed section. This  
    // makes the new values available for reading.
    ConfigurationManager.RefreshSection(sectionName);

    // Set console properties using the  
    // configuration values contained in the  
    // new configuration file.
    Console.BackgroundColor =
      customSection.ConsoleElement.BackgroundColor;
    Console.ForegroundColor =
      customSection.ConsoleElement.ForegroundColor;
    Console.Clear();

    Console.WriteLine();
    Console.WriteLine("Using OpenMappedExeConfiguration.");
    Console.WriteLine("Configuration file is: {0}", 
      config.FilePath);
  }

示例来源:MSDN