[WinForm] C# 讀取文字檔內容

  • 543
  • 0

摘要:WinForm C# 讀取文字檔內容


      #region Get For Reader Compare Data
      /// 
      /// 讀卡後取得出生地代碼
      /// 再與檔案內出生地代碼
      /// 比對,取得出生地名稱
      /// 
      /// 傳入來源檔案
      private void GetDatData(string FILE_NAME)
      {
         try
         {
            using (StreamReader sr = new StreamReader(FILE_NAME, Encoding.GetEncoding("gb2312")))
            {
               while (!sr.EndOfStream)
               {
                  string line = sr.ReadLine();
                  char[] spchar = { ' ' };
                  string[] s_linestr = line.Split(spchar, StringSplitOptions.RemoveEmptyEntries);
                  //[0] : 地區代碼
                  //[1] : 中文名稱
                  for (int i = 0; i < s_linestr.Length; i++)
                  {
                     list_dat.Add(s_linestr[i]);
                     //listBox1.Items.Add(s_linestr[i]);
                     //Console.WriteLine(s_linestr[i]);
                  }
               }
            }
         }
         catch (Exception ex)
         {
            //Console.WriteLine("");
            //Console.WriteLine(ex.Message);
            Func.Log(logPath, logName, "frmICheckIn2_GetDatData(), " +
               "The file could not be read. Exception Message: " + ex.Message
               , PubFunc.LogMsgType.Information);
         }
      }
      #endregion