6

メインexeの.configファイルのuserSettingsセクションに書き込むために.NET2.0でサポートされているAPIはありますか?

シナリオは次のとおりです。

Winforms2.0アプリケーション。

ユーザーレベルのスコープを持つ設定(知る必要がある場合はデータベース接続文字列)があります。これは、ユーザーが設定の値を保存するときに、各ユーザーが.netによって作成されたユーザー.configファイルを持っていることを意味します。

アプリケーションを初めて実行する新規ユーザーの場合、アプリケーションのメインexeの.configファイルのユーザー設定セクションにデフォルト値が含まれています。このセクションは、プロジェクトプロパティの[設定]タブで設定が作成されたときにVisualStudioによって作成されます。

ここで、コンピューター内のすべての管理者ユーザーが新しいユーザーのデフォルト値を変更できるようにします。通常のユーザーにはメインのexeの.configファイルに書き込む権限がないため、管理者のみがこのオプションを使用できます。

ユーザー設定をユーザーの.configファイルに書き込む方法と、メインの.configファイルのappSettingsセクションに書き込む方法を見つけました。しかし、メインの.configのuserSettingsセクションに書き込む方法を見つけようとすると、グーグルが失敗しました。

System.Xmlにフェイルバックして、XmlDocumentに.configを手動でロードする唯一のチャンスはありますか?

4

2 に答える 2

11

いくつかの調査の後、私はこの解決策を思いつきました。これは少し低レベルですが、.config ファイルを手動で解析しなくても .NET 構成 API を通過します。

static void SaveUserSettingDefault(string clientSectionName, string settingName, object settingValue)
{
    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    // find section group
    ConfigurationSectionGroup group = config.SectionGroups[@"userSettings"];
    if (group == null) return;

    // find client section
    ClientSettingsSection clientSection = group.Sections[clientSectionName] as ClientSettingsSection;
    if (clientSection == null) return;

    // find setting element
    SettingElement settingElement = null;
    foreach (SettingElement s in clientSection.Settings)
    {
        if (s.Name == settingName)
        {
            settingElement = s;
            break;
        }
    }
    if (settingElement == null) return;

    // remove the current value
    clientSection.Settings.Remove(settingElement);

    // change the value
    settingElement.Value.ValueXml.InnerText = settingValue.ToString();

    // add the setting
    clientSection.Settings.Add(settingElement);

    // save changes
    config.Save(ConfigurationSaveMode.Full);
} 

次の内容の .config があるとします。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyAssembly.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <MyAssembly.Properties.Settings>
            <setting name="SqlConnectionString" serializeAs="String">
                <value>Server=(local);Database=myDatabase;Integrated Security=true;</value>
            </setting>
        </MyAssembly.Properties.Settings>
    </userSettings>
</configuration>

次のように使用します。

if (RunningAsAdmin) // save value in main exe's config file
{
    SaveUserSettingDefault(@"MyAssembly.Properties.Settings", @"SQLConnectionString", theNewConnectionString);
}
else // save setting in user's config file
{
    Settings.Default. SQLConnectionString = theNewConnectionString;
    Settings.Default.Save();
}
于 2009-03-10T20:37:06.343 に答える
1

はいと思います-.configファイルに手動で書き込むことが唯一の方法です。または、管理者に.configファイルを自分で編集させることもできます。

于 2009-03-10T09:53:47.420 に答える