1편은 너무 쉬웠으리라 생각한다.
그냥 Static Member로 구성된 ConfigurationManager 를 이용해 .NET 에서 Configuratoin 파일에 접근할 수 있도록 제공하는 appSettings Section 에 단순히 접근하는 방법에 대해서만 언급했기 때문에...
뭐 처음 하시는 분은 뭐 생소할 수도 있긴 하겠지만 말이다. 나처럼.. ^^
이제부터는 .NET 에서 제공하는 기본적인 appSettings, connectionStrings 이외의 장소에 내게 필요한 정보들을 Configuration 에 저장하기위한 작업을 해보도록 하자.
음... Configuration 의 Properites 를 정의 하는데는 프로그램적정의하는 방법과 선언적(declarative)으로 정의 하는 방법, 두가지 방법을 사용할 수 있다.
시작하자.
우리는 우리만의 Section을 만들고 그 Section 내에 우리들이 관리하고자 하는 정보들을 넣을 것이다.
어떤 정보인가 보면...
그러므로 먼저 Section 을 구성해야하겠다.
Static 한 Property를 정의하는 것부터 시작해 보자.
먼저 "Configuration" 이라는 클래스라이브러리 프로젝트를 하나 추가하자.
그리고 그 클래스 라이브러리의 속성에 들어가서 NameSpace 를 "Woojja.Configuration" 라 설정하고 저장한다.
아래 그림과 같이...
그리고 Configuration 프로젝트에 자동으로 추가되어있는 Class1.vb 를 삭제하고 "WoojjaConfigurationSection.vb" 라는 이름의 Class 를 하나 추가한다.
그리고 "WoojjaConfigurationSection.vb" 의 내용은 아래와 같다.
1 Imports System
2 Imports System.Configuration
3
4 Public Class WoojjaConfigurationSection
5 Inherits ConfigurationSection
6
7 Private Shared woojjaProperties As ConfigurationPropertyCollection
8
9 Private Shared nameProperty As ConfigurationProperty
10 Private Shared isManProperty As ConfigurationProperty
11 Private Shared birthdayProperty As ConfigurationProperty
12
13 #Region "생성자"
14 Shared Sub WoojjaConfigurationSection()
15 nameProperty = New ConfigurationProperty("nameValue", GetType(String), String.Empty, ConfigurationPropertyOptions.IsRequired)
16 isManProperty = New ConfigurationProperty("isManValue", GetType(Boolean), False, ConfigurationPropertyOptions.None)
17 birthdayProperty = New ConfigurationProperty("birthdayValue", GetType(Date), Nothing, ConfigurationPropertyOptions.None)
18
19 woojjaProperties = New ConfigurationPropertyCollection()
20 woojjaProperties.Add(nameProperty)
21 woojjaProperties.Add(isManProperty)
22 woojjaProperties.Add(birthdayProperty)
23
24 End Sub
25 #End Region
26
27 #Region "Properties"
28 <ConfigurationProperty("nameValue", isrequired:=True)> _
29 Public ReadOnly Property NameValue() As String
30 Get
31 Return CStr(MyBase.Item("nameValue"))
32 End Get
33 End Property
34
35 <ConfigurationProperty("isManValue")> _
36 Public ReadOnly Property isManValue() As Boolean
37 Get
38 Return CBool(MyBase.Item("isManValue"))
39 End Get
40 End Property
41
42 <ConfigurationProperty("birthdayValue")> _
43 Public ReadOnly Property BirthdayValueValue() As Date
44 Get
45 Return DirectCast(MyBase.Item("birthdayValue"), Date)
46 End Get
47 End Property
48 #End Region
49 End Class
그리고 이젠 요렇게 Section을 구성했으니 Configuration 에다가 이런 Section을 사용한다고 선언해야 할 것이다. 요로케...
section name="woojja" type="Woojja.Configuration.WoojjaConfigurationSection, Configuration"
이렇게 되어있는 걸 볼텐데...
name 은 아래 선언할 이름을 나타내고(woojja 라는 태그로 시작한다는 것을 나타낸다.)
type 은 Section 이 정의 되어있는 네임스페이스를 나타내고 assembly 파일 명을 같이 나타낸다.)
클래스 뷰로 확인해 보면
그리고 쉼표다음의 ", Configuration" 는 Configuration 프로젝트를 컴파일하게 되면 만들어지는 assembly 명을 나타낸다.
이젠 잘 되나 한번 보자... ^^
먼저 방금 우리가 추가한 Configuration 프로젝트를 참조추가한다.
이전 1편에서 사용했던 폼을 사용하려 한다.
TextBox 두개와 RadioButton 을 두개 추가하고 버튼도 하나 추가한다.
모양은 이런모양
그리고 Behind Code 를 보게되면
1 Imports System.Configuration
2 Imports Woojja.Configuration
3
4 Public Class Form1
5
6 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
7 Me.TextBox1.Text = ConfigurationManager.AppSettings.Item("woojja").ToString()
8 End Sub
9
10 Private Sub btnGetinfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetinfo.Click
11 Dim woojjaSection As WoojjaConfigurationSection = DirectCast(ConfigurationManager.GetSection("woojja"), WoojjaConfigurationSection)
12 Dim strName As String = woojjaSection.NameValue
13 Dim isMan As Boolean = woojjaSection.isManValue
14 Dim datBirthday As Date = woojjaSection.BirthdayValueValue
15
16 txtName.Text = strName
17 If isMan Then
18 rdoMan.Checked = True
19 Else
20 rdoWoman.Checked = True
21 End If
22 txtbirthday.text = datBirthday.ToString("yyyy-MM-dd")
23 End Sub
24 End Class
자~~ 이젠 F5 를 쿡 눌러보자...
훔.. 잘가져온다.
에러가 발생하는 경우라고 한다면
아래의 그림처럼 app.config 파일의 Section 의 Type을 선언할때 대소문자를 체크해 보아야 할것이고
내용은 길었지만 이번에도 그리 어려운 내용은 아니다.
다음에 진행할 내용도 그리 어려운 내용은 아닐 것이므로 어려워할 필요도 없을 것이다.
빠른시간안에 내용을 진행하도록 하겠다.(벌써 시간이 이렇게 됐네... 아무리 내일이 일요일이지만... ㅡㅡ')
다음 압축파일은 1, 2편에서 사용한 소스가 포함되어있다.
편안한 밤되시고...
행복한 고수되소서..
woojja ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'.NET > VB.NET' 카테고리의 다른 글
[VB.NET] Custom Configuration 을 구성해보자. (4) (0) | 2009.03.24 |
---|---|
[VB.NET] Custom Configuration 을 구성해보자. (3) (0) | 2009.03.14 |
[VB.NET] Custom Configuration 을 구성해보자. (1) (0) | 2009.03.07 |
[VB.NET] Visual Basic LINQ Hands On Labs for Orcas Beta 1 (0) | 2009.03.06 |
[VB.NET] Interop Forms Toolkit 2.0 (0) | 2009.03.06 |