public static void DeleteFile(string filePath, bool contentOnly, bool includingBom) { if (contentOnly) { FileAccess fileAccess = (includingBom) ? FileAccess.Write : FileAccess.ReadWrite; using (var stream = new FileStream(filePath, FileMode.Open, fileAccess)) { int length = 0; if (includingBom) { Encoding encoding = EncodingHelpers.DetectEncoding(stream); if (encoding != null) { length = encoding.Preamble.Length; } } stream.SetLength(length); } } else { File.Delete(filePath); } }
public static bool IsEmptyFile(string path) { var fileInfo = new FileInfo(path); if (fileInfo.Length == 0) { return(true); } if (fileInfo.Length <= 4) { using (FileStream stream = fileInfo.OpenRead()) { Encoding encoding = EncodingHelpers.DetectEncoding(stream); return(encoding?.Preamble.Length == stream.Length); } } return(false); }