从C#App.config文件获取设置 [英] Get setting from C# App.config file
问题描述
我有一个app.config文件.这是从我必须使用的API的示例中获得的...我想从文件中获取设置,以便可以从那里使用设置,而不必重复工作.
I have an app.config file. It's from a sample given to me for an API I have to use... I want to get a setting from the file so that I can use the settings from there and not have to duplicate efforts.
如何在此app.config文件中获得单词"FindMe","LocalMachine"和"My"(以驱动从给定信息中提取证书)?
How can I get the words "FindMe", "LocalMachine" and "My" in this app.config file (to drive pulling a certificate from the given information)?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>...</startup>
<system.serviceModel>
<bindings>...</bindings>
<client>...</client>
<behaviors>
<endpointBehaviors>
<behavior name="ClientCertificateBehavior">
<clientCredentials>
<clientCertificate findValue="FindMe" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
<serviceCertificate><authentication certificateValidationMode="None"/></serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我正在寻找是否可以在System.ServiceModel.Configuration或ConfigurationManager中找到它,但是我没有看到如何获取这些特定值.
I'm looking to see if I can find it in System.ServiceModel.Configuration or ConfigurationManager, but I'm not seeing how to get those specific values.
我认为我真的很亲密,但是我似乎无法获得这些价值.
I think I'm real close, but I can't seem to get the values.
推荐答案
使用Gandarez的评论和Phils的回答作为启动板,我得以为这种解决方案指明了方向.距离完成还很遥远,但是它可以让我获取值,并且可以根据需要对其进行微调:
Using Gandarez's comment and Phils answer as a launching board, I was able to poke my way into this solution. It's far from finished, but it'll allow me to get the values and I can fine tune it as needed:
using System.Configuration;
using System.ServiceModel.Configuration;
using config = System.Configuration.Configuration;
namespace Client
{
public class Program
{
private static void Main(string[] args)
{
config Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup Group = ServiceModelSectionGroup.GetSectionGroup(Config);
BehaviorsSection Behaviors = Group.Behaviors;
EndpointBehaviorElementCollection EndpointBehaviors = Behaviors.EndpointBehaviors;
EndpointBehaviorElement EndpointBehavior = EndpointBehaviors[0];
ClientCredentialsElement ClientCredential = (ClientCredentialsElement) EndpointBehavior[0];
var ClientCertificate = ClientCredential.ClientCertificate;
var findValue = ClientCertificate.FindValue;
var storeName = ClientCertificate.StoreName;
var storeLocation = ClientCertificate.StoreLocation;
var X509FindType = ClientCertificate.X509FindType;
}
}
}
这篇关于从C#App.config文件获取设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!