다음 app.config 파일을 읽고 싶습니다 .. 어떻게 읽나요? 파일을 읽기 위해 변경해야합니까?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Users>
<add username = "Dinesh" password ="Password" domain ="MyCompany" />
<add username = "Kumar" password ="Password" domain ="MyCompany" />
</Users>
</configuration>
섹션을 구현해야한다고 생각합니다.
정확히 원하는대로 샘플 코드를 만들었습니다.
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
namespace ConsoleApplication1
{
public sealed class UsersConfigMapSection : ConfigurationSection
{
private static UsersConfigMapSection config = ConfigurationManager.GetSection("Users") as UsersConfigMapSection;
public static UsersConfigMapSection Config
{
get
{
return config;
}
}
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
private UsersConfigMapConfigElements Settings
{
get { return (UsersConfigMapConfigElements)this[""]; }
set { this[""] = value; }
}
public IEnumerable<UsersConfigMapConfigElement> SettingsList
{
get { return this.Settings.Cast<UsersConfigMapConfigElement>(); }
}
}
public sealed class UsersConfigMapConfigElements : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new UsersConfigMapConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((UsersConfigMapConfigElement)element).Username;
}
}
public sealed class UsersConfigMapConfigElement : ConfigurationElement
{
[ConfigurationProperty("username", IsKey = true, IsRequired = true)]
public string Username
{
get { return (string)base["username"]; }
set { base["username"] = value; }
}
[ConfigurationProperty("password", IsRequired = true)]
public string Password
{
get { return (string)base["password"]; }
set { base["password"] = value; }
}
[ConfigurationProperty("domain", IsRequired = true)]
public string Domain
{
get { return (string)base["domain"]; }
set { base["domain"] = value; }
}
}
}
그런 다음 다음과 같이 구성 파일에서 사용자를 추출합니다.
var users = UsersConfigMapSection.Config.SettingsList.ToList();
마지막으로 구성 파일은 다음과 같아야합니다.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Users" type="ConsoleApplication1.UsersConfigMapSection, ConsoleApplication1"/>
</configSections>
<Users>
<add username = "Dinesh" password ="Password" domain ="MyCompany" />
<add username = "Kumar" password ="Password" domain ="MyCompany" />
</Users>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다