C#_Setup ini File

2015. 6. 30. 12:35C#/코드

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

 

namespace GUI_Form
{
    class Setup_INI
    {
        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

 

        // INI File Read
        // strComPort = CByte(Read_Ini(strIniPath, "RS232", "COMPort"))


        public string Read_INI(string filePath, string section, string key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(section, key, "", temp, 255, filePath);
            return temp.ToString();
        }

 

        // INI File Write
        // Write_Ini(strIniPath, "Inteval", "File Creation Cycle", "100")


        public void Write_INI(string filePath, string section, string key, string val)
        {
            string strError;

            try
            {
                WritePrivateProfileString(section, key, val, filePath);
            }
            catch (Exception exError)
            {
                strError = string.Format("[SYSTEM] : {0}", exError.Message);
            }
        }

        /* True - 지정경로에 폴더 생성, false - 현재 실행파일에 폴더 생성 */
        // 폴더 존재 유무 확인 및 생성
        public string Directory_Create(string strPath, Boolean bRouting)
        {
            /********************************************************
             * strPath     : 경로 지정 및 생성 폴더 이름 설정
             * bRouting    : True 전체 경로 설정 후 폴더 생성,
             *               False 현재 실행 폴더 안에 생성                                                           
            ********************************************************/
            string strReturn = "";

            if (bRouting == true)
            {
                // 지정 경로에 폴더 생성
                DirectoryInfo dir = new DirectoryInfo(strPath);

                if (dir.Exists == false)
                {
                    dir.Create();
                }
                strReturn = strPath;
            }
            else
            {
                // 경로지정을 안했을경우 실행파일 하위 폴더에 생성
                FileInfo fileinfo = new FileInfo(Application.ExecutablePath);
                DirectoryInfo dir = new DirectoryInfo(fileinfo.Directory.FullName + @"\" + strPath);

                /* 경로 설정 */
                string strFullPath = fileinfo.Directory.FullName;

                if (dir.Exists == false)
                {
                    dir.Create();
                }
                strReturn = strFullPath + @"\" + strPath;
            }
            /* 생성 경로 Return - String */
            return strReturn;
        }

        // 파일 생성 및 저장
        public void FileSave(string strFileNamePath, string strSave)
        {

            /********************************************************
             * stgrFileNamePath     : 경로 및 파일 이름
             * strSave              : 저장할 내용                                   
            ********************************************************/

            FileStream fs = new FileStream(strFileNamePath, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);

            sw.WriteLine(strSave);
            sw.Close();
        }

       

#if false
        public string FileOpen()
        {
         
                OpenFileDialog openfiledialog = new OpenFileDialog();
                FolderBrowserDialog folderbrowserdialog = new FolderBrowserDialog();

                string strTemp = null;          
                string strPath = null;

                if (openfiledialog.ShowDialog() == DialogResult.OK)
                {
                    strPath = openfiledialog.FileName;
                }

                StreamReader sr = new StreamReader(new FileStream(strPath, FileMode.Open));

                while (sr.EndOfStream == false)
                {
                    strTemp += sr.ReadToEnd();
                }
                return strTemp;         
        }
#endif

    }
}

'C# > 코드' 카테고리의 다른 글

C#_Resources Image 적용  (0) 2016.04.18
C#_대소문자 및 숫자 구분  (0) 2015.06.30
C#_Deleagate  (0) 2015.06.30
C#_Form Close ( Event )_Serial Port Close  (0) 2015.06.30
C#_OpenFileDialog  (0) 2015.06.30