以下の情報は、組立方法のSystem.Configurationを参照し、設定ファイルを読み込まれ
パブリック クラス設定 { 公共の 静的な 文字列トークン= 「」; 公共の 静的の 文字列のユーザーGetAppConfig =(「ユーザー」); // 指定された設定ファイルの読み取り 専用 の静的の 文字列のファイル= GetFilePathを(); プライベート 静的な 文字列GetFilePath() { 返すシステム。 Windows.Forms.Application.ExecutablePath; } /// <まとめ> /// 戻る* .exe.configのappSettings構成ファイルセクション項目値 ///</要約> /// ます。<param name = "strKey"> </ param>の /// <戻り値> </戻り値> 公共の 静的な 文字列 GetAppConfig(文字列strKey) { コンフィギュレーション設定 = ConfigurationManager.OpenExeConfiguration(ファイル); 戻り設定.AppSettings.Settings [strKey] .Valueの; } /// <まとめ> /// 戻りconnectionNameの接続文字列の名前の文字列データ接続 /// </要約> /// <PARAM NAME = "connectionName"の> </ param>の /// <戻り値> </戻ります> 公共の 静的な 文字列GetConnectionStringsConfig(文字列connectionNameの) { 設定コンフィグ = ConfigurationManager.OpenExeConfiguration(ファイル); 戻りconfig.ConnectionStrings.ConnectionStrings [connectionNameの] .ConnectionString.ToString(); } /// <まとめ> /// ファイル内* .exe.config appSettings構成セクションでは、キーと値のペア追加 /// </要約> /// ます。<param name = "NEWKEY"> </ param>の /// <PARAM NAME = "newValueに"> </ param>の 公共の 静的な 無効 UpdateAppConfigを(文字 NEWKEY、列newValueに) { コンフィギュレーション設定 = ConfigurationManager.OpenExeConfiguration(ファイル); ブールが存在= 偽; foreachの(文字列キーでconfig.AppSettings.Settings.AllKeys) { 場合(キー== NEWKEY) { が存在 = 真。 } } もし(が存在) { config.AppSettings.Settings.Remove(NEWKEY)。 } config.AppSettings.Settings.Add(NEWKEY、newValueに)。 config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(" のappSettings " ); } /// <まとめ> /// アップデート接続ストリング /// </要約> /// <PARAM NAME = "NEWNAME"> 接続文字列名</ param>の /// <PARAM NAME = "newConString"> 接続文字列概要</ param>の /// <PARAM NAME = "newProviderName"> データプロバイダ名</ param>の 公共の 静的な 無効 UpdateConnectionStringsConfig (文字列 NEWNAME、文字列 newConString、文字列newProviderName) { コンフィギュレーションの設定 = ConfigurationManager.OpenExeConfiguration(ファイル); BOOLのが存在= falseに ; // レコードがすでに存在する場合、接続文字列 // 接続文字列を変更したい場合は、既存の IF(config.ConnectionStrings.ConnectionStrings [newnameの] =!ヌル) { が存在 = trueに; } // 接続文字列がすでに存在する場合、それを削除最初の IF (が存在) { config.ConnectionStrings.ConnectionStrings.Remove(NEWNAME); } //接続文字列の新しいインスタンス ConnectionStringSettingsのMySettings = 新新ConnectionStringSettings(NEWNAME、newConString、newProviderName); // 新しい接続文字列のプロファイルに追加します。 Config.ConnectionStrings.ConnectionStrings.Add(MySettings); // 作られたプロファイルの保存変更 config.Save(ConfigurationSaveMode.Modified); // 設定ファイルの強制リロードのconnectionStrings構成セクション ConfigurationManager.RefreshSection(「のconnectionStrings 」); } /// <要約> /// system.serviceModelの最後にすべてのサービスを変更IPアドレスポイント /// のみIPポートを変更しない変更 /// </要約> /// ます。<param name = "サーバIP"> 127.0.0.1 </ param>の 公共の 静的な 無効 UpdateServiceModelConfig(文字列サーバIP) { コンフィギュレーション設定 = ConfigurationManager.OpenExeConfiguration(ファイル); ConfigurationSectionGroup秒 = config.SectionGroups [ " system.serviceModel " ]。 ServiceModelSectionGroup serviceModelSectionGroup =秒としてServiceModelSectionGroup。 ClientSection clientSection = serviceModelSectionGroup.Client。 foreachの(ChannelEndpointElement項目でclientSection.Endpoints) { 文字列パターン= @」\ B \ D {1,3} \ \ D {1,3} \ \ D {1,3} \ \ D {1,3 } \ B " ; 列アドレス= item.Address.ToString()。 文字列置換= ストリング .Format(" {0} " 、サーバIP)。 アドレス = Regex.Replace(アドレス、パターン、置換)。 item.Address = 新しいウリ(アドレス); } config.Save(ConfigurationSaveMode.Modified)。 ConfigurationManager.RefreshSection(" system.serviceModel " ); } /// <要約> /// 加密 /// </要約> /// <PARAM NAME = "送信元"> </ PARAM> /// <PARAM NAME = "E"> </ PARAM> パブリック スタティック 無効暗号化() { コンフィギュレーション設定 = ConfigurationManager.OpenExeConfiguration(ファイル); ConfigurationSection部 = config.GetSection(" のconnectionStrings " )。 もしセクション(!&&! { // section.SectionInformation.ProtectSection( "DataProtectionConfigurationProvider")。 section.SectionInformation.ProtectSection(" RSAProtectedConfigurationProvider " ); config.Save(); } のConfigurationSectionセクション1 = config.GetSection(" のappSettings " )。 もし(!セクション1 = nullを!&& section1.SectionInformation.IsProtected) { // section1.SectionInformation.ProtectSection( "DataProtectionConfigurationProvider"); section1.SectionInformation.ProtectSection(" RSAProtectedConfigurationProvider " ); config.Save(); } } /// <要約> /// 解密 /// </要約> パブリック 静的 ボイド復号化() { 構成設定 = ConfigurationManager.OpenExeConfiguration(ファイル)。 ConfigurationSection部 = config.GetSection(" のconnectionStrings " )。 もし(セクション!= nullを &§ion.SectionInformation.IsProtected) { section.SectionInformation.UnprotectSection()。 config.Save(); } のConfigurationSectionセクション1 = config.GetSection(" のappSettings " )。 もし(!セクション1 = ヌル && section1.SectionInformation.IsProtected) { section1.SectionInformation.UnprotectSection(); config.Save(); } } }
現在アクティブなプロファイルを取得
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile