1

現在、InstallSheild LE を使用してインストールしている Windows サービス (LocalSystem として実行) があります。このサービスは、ローカル データベース ファイルからデータを読み取り、パッケージ化し、設定された間隔で外部サーバーに送信することを目的としています。データベースの場所、サーバーのURLなどをハードコーディングするのではなく、設定ファイルからそれらを読み取りたいと思います。私は App.config で十分に簡単にそれを行うことができますが、私の調査から、App.config (または Program Files 内の任意のファイル) を変更することは、インストール後に困難/不可能であることがわかりました。

私の質問は、「管理者として実行」しなくても、サービスに必要な設定を変更するために実行できるアプリケーションを作成する最良の方法は何でしょうか。これらの設定をレジストリに入れる必要がありますか。それらをAppDataに入れることは正しい答えですか?もしそうなら、それらの設定は設定変更アプリケーションとサービスの間でどのように共有されますか?

私はどちらかというと Web アプリケーション開発者であり、デスクトップ アプリケーション/サービス開発の経験はまだあまりないため、正しい方向性を示していただければ幸いです。

4

1 に答える 1

2

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

于 2013-02-22T22:14:12.633 に答える