天天看點

讀取帶有中文的txt檔案

public string GetLogByDate(string fileName)
        {
            //根據檔案名,判斷檔案是否存在
            string filePath =System.Web.HttpContext.Current.Server.MapPath("~/App_Log/")+ fileName + "-api.log";
            string text = "";         
            //按行讀取為字元串數組
            string[] lines = System.IO.File.ReadAllLines(@filePath,Encoding.Default);
            int i = ;
            int Num = ;
            foreach (string line in lines)
            {
                if (i ==  && Num==)
                {
                    text += "<h4>第1條</h4>";
                }
                if (line != "")
                {
                    text += "<li>" + line + "</li>";
                    Num = ;
                }
                else
                {                  
                    i++;
                    text += "<h4>第" + i + "條</h4>";
                }   
            }
            return text;
        }