블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-19 00:04

'app.config'에 해당되는 글 1건

  1. 2010.07.15 App.Config XML읽고 쓰기..

App.Config XML파일을 읽고 쓰는 간단한 함수를 하나 만들어 봤습니다..

더 좋은 정보 있으면 리플 달아 주세요

 

 

private string AppConfigRead(string keyName)

        {

            string strReturn;

            Configuration currentConfig =

                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 

            if (currentConfig.AppSettings.Settings.AllKeys.Contains(keyName))

                strReturn = currentConfig.AppSettings.Settings[keyName].Value;

            else

                strReturn = ""; //키가없으면.

 

            return strReturn;

        }

 

        private bool AppConfigWrite(string keyName, string value)

        {

            Configuration currentConfig =

                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 

            if (currentConfig.AppSettings.Settings.AllKeys.Contains(keyName)) //키가 있으면

                currentConfig.AppSettings.Settings[keyName].Value = value;

            else       //키가 없으면

                currentConfig.AppSettings.Settings.Add(keyName, value);

           

            currentConfig.Save();

            ConfigurationManager.RefreshSection("appSettings");   // 내용 갱신             

            return true;

        }


출처 : http://www.devpia.com/MAEUL/Contents/Detail.aspx?BoardID=18&MAEULNo=8&no=1699&ref=1699



우선 위 함수를 사용하려면 참조 추가와 namespace 추가가 필요합니다.
1) System.configuration <- 참조 추가
2) using System.Configuration; <- 추가

그렇게 하고도.. 위의 소스에서 Contains 라는걸 찾을 수 없어서 제맘대로 수정했습니다.
정석인진 모르겠지만..돌아가니 참고하실분은 하세요.


        public static string AppConfigRead(string keyName)
        {
            string strReturn = "";
            Configuration currentConfig =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            bool bIsKey = false;

            foreach (string key in currentConfig.AppSettings.Settings.AllKeys)
            {
                if (key.StartsWith(keyName))
                {
                    strReturn = currentConfig.AppSettings.Settings[keyName].Value;
                    bIsKey = true;
                    break;
                }
            }

            if(bIsKey == false)
                return string.Empty;
           
            return strReturn;
        }


        public static bool AppConfigWrite(string keyName, string value)
        {
            Configuration currentConfig =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (currentConfig.AppSettings.Settings.Count != 0)
            {
                bool bIsKey = false;
                foreach (string key in currentConfig.AppSettings.Settings.AllKeys)
                {
                    if (key.StartsWith(keyName))
                    {
                        currentConfig.AppSettings.Settings[keyName].Value = value;
                        bIsKey = true;
                        break;
                    }
                }

                if ( bIsKey == false )       //키가 없으면
                {
                    currentConfig.AppSettings.Settings.Add(keyName, value);
                }
            }
            else
            {
                currentConfig.AppSettings.Settings.Add(keyName, value);
            }

            currentConfig.Save();
            ConfigurationManager.RefreshSection("appSettings");   // 내용 갱신             

            return true;
        }

반응형
Posted by blueasa
, |