private void 寫檔(string fileName,List<string> data)
{
  //寫入資料
  using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName))
  {
     for (int i = 0; i < data.Count; i++)
    { 
       sw.WriteLine(data[i]);
    }
      sw.Close();
      sw.Dispose();
  }
}


      
        private List<string> 讀檔(string filename)
        {
            try
            {
                List<string> data = new List<string>();
                using (System.IO.StreamReader sr = new System.IO.StreamReader(filename))
                {
                    String line;                    
                    while ((line = sr.ReadLine()) != null)
                    {
                        data.Add(line);
                    }
                    sr.Close();                    
                    sr.Dispose();
                }
                return data;


            }
            catch
            {
                return null;
            }
        }

#寫入檔案,保留之前資料,持續寫入 方法1

     private void 文件記錄(string 記錄內容, string 檔案名稱,string 檔案位置)
        {
            System.IO.DirectoryInfo logDirection = new System.IO.DirectoryInfo(檔案位置);
            System.IO.FileInfo logFile = new System.IO.FileInfo(檔案位置+ "\\" + 檔案名稱);
            System.IO.StreamWriter Log;


            if (logDirection.Exists == false)
                logDirection.Create();


            Log = logFile.AppendText();
            Log.WriteLine(記錄內容);
            Log.Flush();
            Log.Close();
            Log.Dispose();
            Log = null;
        }

 

#寫入檔案,保留之前資料,持續寫入 方法2

 File.AppendAllText(檔名, 內容);

arrow
arrow
    全站熱搜

    createps 發表在 痞客邦 留言(0) 人氣()