12

ConfigurationManager を使用して、ユーザー設定を構成ファイルに永続化しようとしています。

管理者権限がないとアプリケーションの変更を Vista/Win 7 に保存できないため、これらの設定をユーザーのみに限定したいと考えています。

これは、Win 7 ([ドライブ]:\Users\[Username]\AppData\Local\[ApplicationName]\[AssemblyName][hash]\[Version\) に保存されているように見えるユーザーの構成を取得しているようです。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);

この構成に変更を保存しようとすると、次の例外が発生します。

InnerException: System.InvalidOperationException
Message="ConfigurationSection properties cannot be edited when locked."
Source="System.Configuration"
StackTrace:
    at System.Configuration.SectionInformation.VerifyIsEditable()
    at System.Configuration.MgmtConfigurationRecord.GetConfigDefinitionUpdates(Boolean requireUpdates, ConfigurationSaveMode saveMode, Boolean forceSaveAll, ConfigDefinitionUpdates& definitionUpdates, ArrayList& configSourceUpdates)

この構成にカスタム ConfigurationSection を追加しようとしました。AppSettingsSection に追加してみました。呼び出すたびにconfig.Save()、上記の例外がスローされます。

何か案は?

Project->Settings デザイナーから ApplicationSettingsBase クラスを使用しようとしましたが、これでカスタム型を保存できるようには見えません。カスタムタイプを保存する機能を備えた同様の機能が必要です。

4

1 に答える 1

8

セクションの SectionInformation.AllowExeDefinition 値を設定する必要があります。

 Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
UserSettings settings;
if ((settings = (UserSettings)configuration.Sections[GENERAL_USER_SETTINGS]) == null)
{
      settings = new UserSettings();
      settings.SectionInformation.AllowExeDefinition =   
                 ConfigurationAllowExeDefinition.MachineToLocalUser;
      configuration.Sections.Add(GENERAL_USER_SETTINGS, settings);
      configuration.Save();
}

デフォルト値は ConfigurationAllowExeDefinition.MachineToApplication で、machine.config と app.exe.config にのみセクションを配置できます。

于 2010-04-13T20:14:15.040 に答える