2013-02-22 32 views
1

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

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

我更Web应用程序开发的和还没有与桌面应用程序/服务开发了丰富的经验,以便在正确的方向上的任何一点,将不胜感激。

+1

特别是对于作为万能的'LocalSystem'帐户运行的服务,我会非常警惕具有通过非管理员可写设置。我认为,从安装和升级的角度来看,不要编辑存储在Program Files中的文件是一个很好的决定,但我建议在编辑应用程序中嵌入一个清单,以要求管理员权限运行它,并使其应用对文件,注册表项或任何用于存储的任何非常严格的ACL。 – shambulator 2013-02-22 23:41:22

+0

有道理。你能否指出我关于嵌入清单和设置ACL的任何文档的方向? – jdehlin 2013-02-23 19:54:50

回答

1

您可以找到App.Config中的应用程序之外的安装目录,并将其放置在一个共同的文件夹(如应用程序数据)。然后你会告诉你的应用程序从那里加载它,而不是从应用程序安装目录中取出它。

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

+0

我相信这就是我要找的。谢谢! – jdehlin 2013-02-22 22:54:24

+0

这个回应指出了我正确的方向,但提供的示例代码将配置复制到相同的目录中(在Program Files中)。我用下面把它在ProgramData(未应用程序数据,它是用户特定的)'VAR CONFIGFILE = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)+ “\\” + AppName的+ “\\ App.config中”;'。您可以使用SpecialFolder.ApplicationData而不是SpecialFolder.CommonApplicationData来进行用户特定的配置。 – jdehlin 2013-02-25 13:42:29