简体   繁体   English

如何在C#中更新app.config

[英]How to update app.config in c#

My project is developing in WPF with MVVM. 我的项目是使用MVVM在WPF中开发的。 In viewModel consuming WCF services. 在viewModel中使用WCF服务。 In View app.config file is present and it contains information about WCF configurations like endpoint info... . 在View中,存在app.config文件,其中包含有关WCF配置的信息,例如端点信息...。 We have two services like service1 and service2 both are identical having function but the service address will be different. 我们有两个服务,例如service1和service2都具有相同的功能,但服务地址将不同。 Service1 is communicating with DataBase1 and the service2 is communicating with Database2, so service to DB is one to one mapping. Service1正在与数据库1通信,而service2正在与数据库2通信,因此对DB的服务是一对一的映射。 In view a ComboBox is there which will show two option like DB1 and DB2. 在视图中,有一个ComboBox,它将显示两个选项,例如DB1和DB2。 Based the comboBox selection i have to update my app.config file [endpoint address] as per WPF-MVVM pattern. 基于comboBox的选择,我必须根据WPF-MVVM模式更新我的app.config文件[端点地址]。 Because application should re-init with new service - DB function call. 因为应用程序应该使用新服务重新初始化-DB函数调用。 So, If ComboBox selection changed where command will invoke which is (command) is written in ViewModel. 因此,如果更改了ComboBox选择,那么将在ViewModel中编写命令(命令)的调用位置。 So how to update config file? 那么如何更新配置文件?

Else is there anyother way to achieve this? 还有其他方法可以实现这一目标吗?

配置必须在您的启动项目中,这可能是问题所在

You have two identical WCF services, and you want the user to use a ComboBox to choose which one your application interacts with. 您有两个相同的WCF服务,并且希望用户使用ComboBox选择与您的应用程序进行交互的对象。 So your application only needs a single definition for the proxy in the config file. 因此,您的应用程序只需要在配置文件中为代理定义一个定义。 It does not matter which of the two URLs this is pointing at. 指向两个URL中的哪个都无所谓。

You can programmatically set the URL of the endpoint at runtime in the constructor of the proxy. 您可以在运行时在代理的构造函数中以编程方式设置端点的URL。 I can't tell from the question what protocol you're using but there are some examples here of how to do this. 我不能从问题中分辨出您使用的是哪种协议,但是这里有一些示例

You need to keep a list of what the possible URL values are, to allow the user to make a choice (but this is different from which one has been selected, I'll get to that later). 您需要保留一个可能的URL值列表,以允许用户做出选择(但这与选择的URL值不同,我将在后面进行介绍)。 I'd suggest having the alternative URLs in config, in this part of the file (I'm going to guess at the http protocol, just so you can see what I mean)... 我建议在文件的这一部分中,在配置中使用备用URL(我将猜测http协议,以便您可以理解我的意思)...

<configuration>
    ...
    <appSettings>
         <add key="Endpoint1" value="http://whatever"/>
         <add key="Endpoint2" value="http://whatever"/>
    </appSettings>
    ...
</configuration>

Then on startup, iterate through these to put them in your ComboBox: 然后在启动时,遍历这些内容,将它们放入您的ComboBox中:

var appSettings = ConfigurationManager.AppSettings;
foreach (var key in appSettings.AllKeys.Where(x=>x.StartsWith("Endpoint"))
{
    // put appSettings[key] into the combobox (in a list in your viewmodel if you use MVVM)
}

Then you need to store which one of these have been selected by this user (and remember to pre-select that value on startup). 然后,您需要存储该用户选择了其中之一(并记住在启动时预先选择该值)。 I'd suggest using the "user settings", which (unlike the "App.config" file) allows the values to be changed. 我建议使用“用户设置”,该设置(与“ App.config”文件不同)允许更改值。 See User Settings in C# for how to do that. 有关如何操作,请参见C#中的用户设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM