2013-02-22 39 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