2009-03-10 9 views
6

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

シナリオは、

Winforms 2.0アプリケーションです。

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

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

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

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

私の唯一のチャンスはSystem.Xmlに戻って失敗し、手動で.configをXmlDocumentにロードしますか?

答えて

10

いくつかの調査の後、私はこの解決策を考え出しました。これは少し低いレベルですが、.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(); 
} 
+0

残念ながら、このアプローチは 'userSettings'の継承された設定では動作しません。 – Tarik

1

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

+0

、管理者がコンピュータに精通した人ではありません。彼は自分のマシン(例えば、ほぼすべての家庭のXPユーザー)でAdminとして実行されている単なるユーザーです。しかし、あなたの提案は別のシナリオでは役に立ちます。ありがとう。 –

関連する問題