코드 예제입니다

 


 

텍스트 파일 읽기 


 

 


 

using System.IO;


 

    private static bool GetLineStrAfterBlankLine(StreamReader sr, out string nonblankst)
    {
        nonblankst = "";

        bool rtn = true;
        try
        {
            string st;
            while (true)
            {
                if (sr.EndOfStream)
                {
                    rtn = false;
                    break;
                }

                st = sr.ReadLine();
                if (st.Trim().Length > 0)
                {
                    nonblankst = st.Trim();
                    break;
                }
            }
        }
        catch
        {
            rtn = false;
        }
        return rtn;
    }


 

    public static bool ReadFile(string fname)
    {
        bool rtn = true;

        try
        {
            string st;
            StreamReader sr = new StreamReader(fname, System.Text.Encoding.Default);

            //---------------------------------------------
            //  한줄씩 읽기
            //---------------------------------------------
            bool success;
            int ipos;

            while (true)
            {
                //~~ 한줄 읽기
                success = GetLineStrAfterBlankLine(sr, out st);
                if (success == false)
                {
                    rtn = false;
                    goto ErrHandler;
                }
                //~~ END mark
                if (st == "[END]") break;

                //~~ 내용 판독
                ipos = st.IndexOf("<필요스트링>");
                if (ipos >= 0)
                {
                }
            }
        ErrHandler:
            sr.Close();
        }
        catch
        {
            rtn = false;
        }

        return rtn;
    }

 

 


파일 쓰기


 


 

 

using System.IO;


 

        //=====================================================================
        //  파일에 쓰기
        //=====================================================================
        public static bool WriteFile(string st)
        {
            bool rtn = true;
            try
            {
                DateTime now = DateTime.Now;

                string direc = "D:\\Event";
                string direcMon = direc + "\\" + String.Format("{0:0000}{1:00}", now.Year, now.Month);
                string fname = direcMon + "\\" + "FileName" +
                    String.Format("{0:0000}{1:00}{2:00}", now.Year, now.Month, now.Day) + ".log";

                if (!Directory.Exists(direc)) Directory.CreateDirectory(direc);
                if (!Directory.Exists(direcMon)) Directory.CreateDirectory(direcMon);

                bool append = true;
                StreamWriter sw = new StreamWriter(fname, append);

                string tmp1, tmp2;
                tmp1="["+String.Format("{0:0000}.{1:00}.{2:00}", now.Year, now.Month, now.Day)+"]";
                tmp2="["+String.Format("{0:00}:{1:00}:{2:00}", now.Hour, now.Minute, now.Second)+"]"+" ";
                sw.WriteLine(tmp1 + tmp2 + st);

                sw.Close();
            }
            catch
            {
                rtn = false;
            }
            return rtn;
        }

Posted by 마스샘