123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MECF.Framework.Common.Utilities
- {
- public class FileHelper
- {
- //定义一个用于保存静态变量的实例
- private static FileHelper instance = null;
- //定义一个保证线程同步的标识
- private static readonly object locker = new object();
- //构造函数为私有,使外界不能创建该类的实例
- private FileHelper() { }
- public static FileHelper Instance
- {
- get
- {
- if (instance == null)
- {
- lock (locker)
- {
- if (instance == null) instance = new FileHelper();
- }
- }
- return instance;
- }
- }
- public void SaveNewLineDataToTxt(string dir, string filePath, string text, bool isAppend = false)
- {
- if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
- filePath = filePath + ".txt";
- using (StreamWriter sw = new StreamWriter(filePath, isAppend, Encoding.UTF8))
- {
- sw.WriteLine(text);
- sw.Flush();
- sw.Close();
- }
- }
- public void SaveNewLineDataToTxt(string fullFilePath, string text, bool isAppend = false)
- {
- string dir = fullFilePath.Substring(0, fullFilePath.LastIndexOf('\\'));
- if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
- using (StreamWriter sw = new StreamWriter(fullFilePath, isAppend, Encoding.UTF8))
- {
- sw.WriteLine(text);
- sw.Flush();
- sw.Close();
- }
- }
- public void SaveText(string fullFileName, string WriteTxt)
- {
- try
- {
- string dir= fullFileName.Substring(0, fullFileName.LastIndexOf('\\'));
- if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
- FileStream fs = new FileStream(fullFileName,
- FileMode.OpenOrCreate, FileAccess.Write);
- StreamWriter streamWriter = new StreamWriter(fs, Encoding.UTF8);
- streamWriter.Flush();
- streamWriter.BaseStream.Seek(0, SeekOrigin.End);
- streamWriter.WriteLine(WriteTxt);
- streamWriter.Flush();
- streamWriter.Close();
- }
- catch { }
- }
- public void SaveText(string dir, string fileName, string WriteTxt)
- {
- try
- {
- if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
- FileStream fs = new FileStream(dir + "\\" + fileName + ".txt",
- FileMode.OpenOrCreate, FileAccess.Write);
- StreamWriter streamWriter = new StreamWriter(fs, Encoding.UTF8);
- streamWriter.Flush();
- streamWriter.BaseStream.Seek(0, SeekOrigin.End);
- streamWriter.WriteLine(WriteTxt);
- streamWriter.Flush();
- streamWriter.Close();
- }
- catch { }
- }
- public string ReadAllText(string filePath)
- {
- if (!File.Exists(filePath)) return filePath + ",文件不存在";
- return File.ReadAllText(filePath);
- }
- public string ReadFileStream(string filePath)
- {
- string result = string.Empty;
- try
- {
- if (!File.Exists(filePath)) return filePath + ",文件不存在";
- FileStream fileStream = new FileStream(filePath, FileMode.Open,
- FileAccess.ReadWrite, FileShare.ReadWrite);
- StreamReader sr = new StreamReader(fileStream, Encoding.Default);
- result = sr.ReadToEnd();
- sr.Close();
- fileStream.Close();
- }
- catch { return result; }
- return result;
- }
- }
- }
|