讀取、重寫txt文件裏的內容()

 //讀取txt文件的內容

 public static string GettfCount(string strfile)
    {
        string strout;
        strout = "";
        if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(strfile)))
        {
            strout = "文件出錯!聯繫管理員";
        }
        else
        {
            StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(strfile), System.Text.Encoding.Default);
            String input = sr.ReadToEnd();
            sr.Close();
            strout = input;
        }
        return strout;
    }

  //重寫
    public static void addTFCount(string strfile,string value)
    {
        string path = System.Web.HttpContext.Current.Server.MapPath(strfile);                  
                  FileStream fs = new FileStream (path,FileMode.OpenOrCreate,FileAccess.Write);
                  StreamWriter m_streamWriter = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));	
            m_streamWriter.Write(value); 
            m_streamWriter.Flush(); 
            m_streamWriter.Close(); 
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章