1

どのexeにも関係のない設定ファイルを読み書きする必要があります。私はこれを試しています:

        var appConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = "SlamDunkSuper.config" }, ConfigurationUserLevel.None);
        if(appConfiguration == null) {

           //Configuration file not found, so throw an exception
           //TODO: thow an exception here
        } else {

           //Have Configuration, so work on the contents
           var fileEnvironment = appConfiguration.GetSection("fileEnvironment");
        }

例外はスローされませんが、fileEnvironment は常に null です。ファイルの内容は次のとおりです。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
      <section name="fileEnvironment" type="System.Configuration.NameValueSectionHandler"/>
   </configSections>

   <fileEnvironment>
      <add key="DxStudioLocation" value="123456"/>
   </fileEnvironment>
</configuration>

誰か私を荒野から連れ出してください。また、セクションの内容を取得した後で、NameValueCollection のエントリを作成または変更する方法もわかりません。ありがとう

4

2 に答える 2

0

AppSettingsSectionをいくつかの小さな調整でグローバル化できます。

<section name="fileEnvironment" type="System.Configuration.AppSettingsSection"/>

消費する:

        var appConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = "SlamDunkSuper.config" }, ConfigurationUserLevel.None);

        if (!appConfiguration.HasFile) // no need to null check, ConfigurationManager.OpenMappedExeConfiguration will always return an object or throw ArgumentException
        {
            //Configuration file not found, so throw an exception
        }
        else
        {
            var section = appConfiguration.GetSection("fileEnvironment") as AppSettingsSection;
            if (section != null)
            {
                var dxStudioLocation = section.Settings["DxStudioLocation"].Value;
            }
        }
于 2012-01-21T23:02:41.127 に答える
-2

.net では、実行中の exe ファイルによって構成ファイルが選択されるため、5 つのプロジェクト (4 つの dll と 1 つの exe) があり、exe ファイルからアプリを実行するときに各プロジェクトに異なる構成ファイルがある場合、その dll は彼がロードしたファイルは、exe の構成ファイルが自分の構成ファイルであると見なします。

つまり、dll プロジェクトの構成ファイルを読み取るには、パスを使用して明示的に開く必要があります。

それが役に立てば幸い

于 2012-01-21T09:50:45.630 に答える