FileWrap
Represents a class object wrapper for the File class.
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
namespace Relativity.DataExchange.Io
{
[CLSCompliant(false)]
internal class FileWrap : IFile
{
private readonly IPath instance;
internal FileWrap(IPath path)
{
if (path == null)
throw new ArgumentNullException("path");
instance = path;
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public void Copy(string sourceFileName, string destFileName)
{
sourceFileName = instance.NormalizePath(sourceFileName);
destFileName = instance.NormalizePath(destFileName);
try {
File.Copy(sourceFileName, destFileName);
} catch (Exception) {
File.Copy(sourceFileName, destFileName);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public void Copy(string sourceFileName, string destFileName, bool overwrite)
{
sourceFileName = instance.NormalizePath(sourceFileName);
destFileName = instance.NormalizePath(destFileName);
try {
File.Copy(sourceFileName, destFileName, overwrite);
} catch (Exception) {
File.Copy(sourceFileName, destFileName, overwrite);
}
}
public int CountLinesInFile(string path)
{
using (StreamReader streamReader = new StreamReader(path)) {
int num = 0;
while (streamReader.ReadLine() != null) {
num++;
}
return num;
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public FileStream Create(string path)
{
path = instance.NormalizePath(path);
try {
return File.Create(path);
} catch (Exception) {
return File.Create(path);
}
}
public FileStream Create(string path, bool append)
{
FileMode mode = append ? FileMode.Append : FileMode.Create;
FileAccess access = append ? FileAccess.Write : FileAccess.ReadWrite;
return Create(path, mode, access, FileShare.None);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public FileStream (string path, FileMode mode, FileAccess access, FileShare share)
{
path = instance.NormalizePath(path);
try {
return new FileStream(path, mode, access, share);
} catch (Exception) {
return new FileStream(path, mode, access, share);
}
}
public StreamWriter CreateText(string path)
{
path = instance.NormalizePath(path);
return File.CreateText(path);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public void Delete(string path)
{
path = instance.NormalizePath(path);
try {
if (File.Exists(path))
File.Delete(path);
} catch (Exception) {
if (File.Exists(path))
File.Delete(path);
}
}
public bool Exists(string path)
{
path = instance.NormalizePath(path);
return File.Exists(path);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public long GetFileSize(string fileName)
{
fileName = instance.NormalizePath(fileName);
try {
return new FileInfo(fileName).Length;
} catch (Exception) {
return new FileInfo(fileName).Length;
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public void Move(string sourceFileName, string destFileName)
{
try {
File.Move(sourceFileName, destFileName);
} catch (Exception) {
File.Move(sourceFileName, destFileName);
}
}
public string ReadAllText(string path)
{
path = instance.NormalizePath(path);
return File.ReadAllText(path);
}
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "This is OK.")]
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The try/catch retry implementation is for backwards compatibility only.")]
public FileStream ReopenAndTruncate(string fileName, long length)
{
try {
FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
fileStream.SetLength(length);
return fileStream;
} catch (Exception) {
FileStream fileStream2 = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
fileStream2.SetLength(length);
return fileStream2;
}
}
}
}