1. 程式人生 > >C#檔案讀寫追加建立目錄,判斷目錄是否存在等操作

C#檔案讀寫追加建立目錄,判斷目錄是否存在等操作

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FundData
{
    static class FileUtil
    {
        public static void Save(string filename, string data)
        {
            FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(data);
            sw.Close();
            fs.Close();
        }

        public static void Append(string filename, string data)
        {
            FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(data);
            sw.Close();
            fs.Close();
        }

        public static string Load(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            var s = new StreamReader(fs);
            var data = s.ReadToEnd();
            s.Close();
            fs.Close();
            return data;
        }

        public static bool IsDirectoryExist(string path)
        {
            return Directory.Exists(path);
        }

        public static void CreateDirectory(string path)
        {
            Directory.CreateDirectory(path);
        }
    }
}