.net 創建或修改txt文件

  public static bool WriteFile(string filePath, string fileName, string content)
        {
            FileStream fs = null;
            string fileUrl = MergePath("/", new string[] { filePath, fileName });
            string physicalPath = HttpContext.Current.Server.MapPath(fileUrl);

            try
            {
                CreateDirectory(HttpContext.Current.Server.MapPath(filePath));

                if (FileExists(fileUrl))
                {
                    fs = new FileStream(physicalPath, FileMode.OpenOrCreate, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(physicalPath, FileMode.Append, FileAccess.Write);
                }

                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                sw.WriteLine(content);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                LogHelper.Error("關注被動回覆生成txt文件失敗,異常:" + ex);
            }
            finally
            {
                fs.Close();
            }

            if (FileExists(fileUrl))
            {
                return true;
            }

            return false;
        }

 

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