C# 寫入檔案

摘要:C# 寫入檔案

常用, 所以在這裡記一下

using System.IO;
using System.Text;

namespace Test
{
    public class SaveData
    {
         string path = @"./export/";
         public void Save(string fileName, string text)
         {
             if (!System.IO.Directory.Exists(path))
             {
                 System.IO.Directory.CreateDirectory(path);
             }
             if (!System.IO.File.Exists(path + fileName))
             {
                 FileStream fs = System.IO.File.Create(path + fileName);
                 fs.Close();
             }
             using (StreamWriter w = System.IO.File.AppendText(path + fileName))
             {
                 //System.IO.File.SetAttributes(path + fileName, FileAttributes.Hidden);//隱藏
                 w.WriteLine(text, Encoding.Default);
             }
         }

         public void DeleteFile(string fileName)
         {
             File.Delete(path + fileName);
         }
    }
}