2012-03-28 6 views
2

Visual Studio 2005를 사용하고 있는데 "App.config"파일과 함께 하나의 응용 프로그램을 만들었습니다. 내가C에서 App.config 파일을 구성하는 방법

내 app.config 파일에 포함 된 ... 편집하고 오류가 저를 도와주세요 보여줍니다 해당 파일의 App.config에 새 값을 추가 할 때 :

을 :

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="keyvalue" value="value"/> 
    <add key="keyvalue1" value="value1"/> 
</appSettings> 
<mySettings> 
    <add name="myname" myvalue="value1"/> 
</mySettings> 
</configuration> 

그것은 같은 오류를 보여줍니다

Could not find schema information for the element "mySettings" 
Could not find schema information for the element "add" 
Could not find schema information for the element "myvalue" 
+1

이러한 '오류'는 정보 메시지 일 뿐이라는 것에 유의하십시오. Visual Studio에서는 이러한 값을 요소, 특성, 저장해야하는 형식으로 가정하면 제대로 작동하지 않는다는 것을 알립니다. 이를 무시하는 것이 안전하지만 제공되는 응답은 이러한 사용자 지정 값을 읽는 방법을 구현하는 방법에 대한 지침을 제공합니다. –

답변

6

"MySettings"그룹을 만들지 마십시오. AppSettings 그룹에 필요한 것을 넣으십시오.

사용자는 mySettings 그룹을 만들 수 있지만 사용자 정의 (비표준) 구성 섹션을 포함하는 경우 here 또는 here과 같이 configSections 요소에 선언해야합니다.

정상적인 표준을 따르는 것이 더 좋기 때문에 사용자 정의 섹션을 추가해야하는 실질적인 이유가 없으면 첫 번째 대답과 함께해야합니다. 미래의 유지 보수 프로그래머가 쉽게 이용할 수 있습니다.

+0

감사합니다. @DavidStratton .. – Ramesh

3

당신은 일반적인 구성 파일의 일부가 아닌 새로운 섹션 정의됩니다

<mySettings> 
    <add name="myname" myvalue="value1"/> 
</mySettings> 

자신의 섹션을 통합하기를, 당신은 당신의 특정 섹션을 읽을 수있는 뭔가를 작성해야합니다.

<configuration> 
    <configSections> 
     <section name="mySettings" type="MyAssembly.MySettingsConfigurationHander, MyAssembly"/> 
    </configSections> 
    <!-- Same as before --> 
</configuration> 

예제 코드 샘플은 다음과 같습니다 : 당신은 다음과 같은 부분을 처리 할 핸들러에 대한 참조를 추가

public class MySettingsSection 
{ 
    public IEnumerable<MySetting> MySettings { get;set; } 
} 

public class MySetting 
{ 
    public string Name { get;set; } 
    public string MyValue { get;set; } 
} 

public class MySettingsConfigurationHander : IConfigurationSectionHandler 
{ 
    public object Create(XmlNode startNode) 
    { 
      var mySettingsSection = new MySettingsSection(); 

      mySettingsSection.MySettings = (from node in startNode.Descendents() 
             select new MySetting 
             { 
              Name = node.Attribute("name"), 
              MyValue = node.Attribute("myValue") 
             }).ToList(); 

     return mySettingsSection; 
    } 
} 

public class Program 
{ 
    public static void Main() 
    { 
     var section = ConfigurationManager.GetSection("mySettings") as MySettingsSection; 

     Console.WriteLine("Here are the settings for 'MySettings' :"); 

     foreach(var setting in section.MySettings) 
     { 
      Console.WriteLine("Name: {0}, MyValue: {1}", setting.Name, setting.MyValue); 
     } 
    } 
} 

구성 파일을 읽을 수있는 다른 방법이 있습니다 만 이것은 자유형으로 타이핑하는 단순한 사람이었습니다.

+0

+1 노력을 투입하고 코드를 보여줍니다. – David

+0

@Dominic Zukiewicz ... 고맙습니다. 제게는 유용합니다 .. – Ramesh

관련 문제