d

ini 파일 안의 모든 section 값과 해당하는 section의 모든 key 값을 한번에 가져오기 위한 소스

 

Form 클래스 안에

 

        [DllImport("kernel32")]
        static extern int GetPrivateProfileString(string Section, string Key,
               string Value, StringBuilder Result, int Size, string FileName);

     
        [DllImport("kernel32")]
        static extern int GetPrivateProfileString(string Section, int Key,
               string Value, [MarshalAs(UnmanagedType.LPArray)] byte[] Result,
               int Size, string FileName);


        [DllImport("kernel32")]
        static extern int GetPrivateProfileString(int Section, string Key,
               string Value, [MarshalAs(UnmanagedType.LPArray)] byte[] Result,
               int Size, string FileName);

 

        public string path;

 

        public string[] GetSectionNames()  // ini 파일 안의 모든 section 이름 가져오기
        {
            for (int maxsize = 500; true; maxsize *= 2)
            {
                byte[] bytes = new byte[maxsize];
                int size = GetPrivateProfileString(0, "", "", bytes, maxsize, path);

            
                if (size < maxsize - 2)
                {
                    string Selected = Encoding.ASCII.GetString(bytes, 0, size - (size > 0 ? 1 : 0));
                    return Selected.Split(new char[] { '\0' });
                }
            }
        }  

 

        public string[] GetEntryNames(string section)   // 해당 section 안의 모든 키 값 가져오기
        {
            for (int maxsize = 500; true; maxsize *= 2)
            {
                byte[] bytes = new byte[maxsize];
                int size = GetPrivateProfileString(section, 0, "", bytes, maxsize, path);

                if (size < maxsize - 2)
                {
                    string entries = Encoding.ASCII.GetString(bytes, 0, size - (size > 0 ? 1 : 0));
                    return entries.Split(new char[] { '\0' });
                }
            }
        }  

'C#' 카테고리의 다른 글

[C#] 서버 상태 체크  (0) 2016.08.31
[Windows Service] 디버깅  (0) 2016.08.05
[Windows Service] 파일 읽기  (0) 2016.07.14
[C#] 파일 경로  (0) 2016.07.14
[C#] DataGridView 크기 조절  (0) 2016.07.08
블로그 이미지

_Able

,