ASP.NET(C#)文件讀寫函數

using System;
using System.Data;
using System.IO;
using System.Text;

public void WriteFile(string content, string fileSavePath)
{
           if (System.IO.File.Exists(fileSavePath))
        {
            System.IO.File.Delete(fileSavePath);
        }
        using (System.IO.FileStream fs = System.IO.File.Create(fileSavePath))
        {
            byte[] info = new System.Text.Encoding.GetEncoding("gb2312").GetBytes(content);   //防止亂碼
            fs.Write(info, 0, info.Length);
        }
}
public string ReadFile(string fileOpenPath)
{
           if (!System.IO.File.Exists(fileOpenPath))
        {
            return null;
        }
        using (System.IO.StreamReader sr = System.IO.File.OpenText(fileOpenPath))
        {
            return  sr.ReadToEnd().ToString();
        }
}

發佈了8 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章