private FunctionResult Execute(string filePath, DoesNotExistOptions fileDoesNotExist, ExistOptions fileExists, TextCodepage codepage = TextCodepage.Default, bool isText = true) { FunctionExecutor tester = (new FunctionTester <FileOpen.FileOpen>()).Compile( new PropertyValue(FileOpenShared.IsTextPropertyName, isText), new PropertyValue(FileOpenShared.CodepagePropertyName, codepage), new PropertyValue(FileOpenShared.FileDoesNotExistPropertyName, fileDoesNotExist), new PropertyValue(FileOpenShared.FileExistsPropertyName, fileExists)); var result = tester.Execute(new ParameterValue(FileOpenShared.FilePathPropertyName, filePath)); Assert.IsFalse(FileHelpers.IsFileLocked(filePath), "File must not open before the execution path is active."); return(result); }
private void GetWriteOptions(string filePath, DoesNotExistOptions fileDoesNotExist, ExistOptions fileExist, out string newFilePath, out FileMode fileMode) { newFilePath = filePath; if (System.IO.File.Exists(filePath)) { Log(string.Format("File at {0} exists.", filePath)); switch (fileExist) { case ExistOptions.AppendData: fileMode = FileMode.Append; break; case ExistOptions.IncrementFileName: fileMode = FileMode.CreateNew; newFilePath = filePath.GetIncrementalFileName(); break; case ExistOptions.OverwriteFile: fileMode = FileMode.Truncate; break; case ExistOptions.ThrowException: throw new IOException(String.Format("File [{0}] already exists.", filePath)); default: throw new Exception(String.Format("Invalid ExistOptions [{0}] specified.", fileExist)); } } else { Log(string.Format("File at {0} does not exist.", filePath)); switch (fileDoesNotExist) { case DoesNotExistOptions.CreateFile: string directoryName = Path.GetDirectoryName(newFilePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); Log(string.Format("Created directory {0}.", directoryName)); } fileMode = FileMode.CreateNew; break; case DoesNotExistOptions.ThrowException: throw new FileNotFoundException(String.Format("File [{0}] does not exist.", filePath)); default: throw new Exception(String.Format("Invalid ExistOptions [{0}] specified.", fileDoesNotExist)); } } }
public bool TryPrepareForWrite(DoesNotExistOptions fileDoesNotExist, ExistOptions fileExist) { if (isPrepared) { return(false); } string newFilePath; GetWriteOptions(FilePath, fileDoesNotExist, fileExist, out newFilePath, out fileMode); FilePath = newFilePath; fileAccess = System.IO.FileAccess.Write; isPrepared = true; return(true); }
private FunctionResult Execute( ExistOptions fileExists, DoesNotExistOptions fileNotExist, string filePathProp, object contentsProp ) { FunctionExecutor tester = new FunctionTester <BinaryFileWrite>().Compile( new PropertyValue(BinaryFileWriteShared.FileExistsPropertyName, fileExists), new PropertyValue(BinaryFileWriteShared.FileDoesNotExistPropertyName, fileNotExist) ); var result = tester.Execute( new ParameterValue(FileShared.FilePathPropertyName, (BinaryFileHandle)filePathProp), new ParameterValue(BinaryFileWriteShared.ContentsPropertyName, contentsProp) ); Assert.IsFalse(FileHelpers.IsFileLocked(result.Value)); return(result); }
public static string WriteFile(BinaryFileHandle fileHandle, bool closeFileHandle, IEnumerable <byte> contents, DoesNotExistOptions fileDoesNotExist, ExistOptions fileExist, Action <string> logger) { fileHandle.TryPrepareForWrite(fileDoesNotExist, fileExist); try { byte[] arrayContents = contents.ToArray(); var fileStream = fileHandle.GetFileStream(); logger(string.Format("Writing {0} bytes.", arrayContents.Length)); fileStream.Write(arrayContents, 0, arrayContents.Length); } finally { if (closeFileHandle) { fileHandle.Close(); } } return(fileHandle.FilePath); }
public FileOpenX(string filePath, bool isText, TextCodepage codepage, DoesNotExistOptions fileDoesNotExist, ExistOptions fileExists, Action <string> logger) { FileHandle = isText ? (FileHandle) new TextFileHandle(filePath, codepage) : (FileHandle) new BinaryFileHandle(filePath); FileHandle.LogEvent += message => logger(message); FileHandle.TryPrepareForWrite(fileDoesNotExist, fileExists); }
public static string WriteFile(TextFileHandle fileHandle, bool closeFileHandle, string contents, DoesNotExistOptions fileDoesNotExist, ExistOptions fileExist, TextCodepage destinationCodepage, Action <string> logger) { if (fileHandle.TryPrepareForWrite(fileDoesNotExist, fileExist)) { fileHandle.TextCodepage = destinationCodepage; } StreamWriter writer = fileHandle.CreateStreamWriter(); try { logger(string.Format("Writing <{0}>", contents)); writer.Write(contents); writer.Flush(); } finally { if (closeFileHandle) { fileHandle.Close(); } } return(fileHandle.FilePath); }