private void CleanupLogFileIfNeeded() { try { FileInfoBase logFile = FileSystemHelpers.FileInfoFromFileName(_logFilePath); if (logFile.Length > MaxContinuousLogFileSize) { // lock file and only allow deleting it // this is for allowing only the first (instance) trying to roll the log file using (File.Open(_logFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete)) { // roll log file, currently allow only 2 log files to exist at the same time string prevLogFilePath = GetLogFilePath(JobPrevLogFileName); FileSystemHelpers.DeleteFileSafe(prevLogFilePath); logFile.MoveTo(prevLogFilePath); Action rollEventHandler = RolledLogFile; if (rollEventHandler != null) { rollEventHandler(); } } } } catch { // best effort for this method } }
/// <summary> /// Move file to the tobedeleted directory /// </summary> /// <param name="fileToMove">The file to be moved</param> /// <returns>true if action was successful otherwise false</returns> private bool TryMoveFileToBeDeleted(FileInfoBase fileToMove) { try { if (!Directory.Exists(_toBeDeletedDirectoryPath)) { Directory.CreateDirectory(_toBeDeletedDirectoryPath); } fileToMove.MoveTo(Path.Combine(_toBeDeletedDirectoryPath, fileToMove.Name + "." + Path.GetRandomFileName())); return(true); } catch { return(false); } }
public void MockFileInfo_MoveTo_ShouldUpdateFileInfoDirectoryAndFullName() { // Arrange MockFileSystem fileSystem = new MockFileSystem(); fileSystem.AddFile(XFS.Path(@"c:\temp\file.txt"), new MockFileData(@"line 1\r\nline 2")); FileInfoBase fileInfo = fileSystem.FileInfo.FromFileName(XFS.Path(@"c:\temp\file.txt")); // Act string destinationFolder = XFS.Path(@"c:\temp2"); string destination = XFS.Path(destinationFolder + @"\file.txt"); fileSystem.AddDirectory(destination); fileInfo.MoveTo(destination); Assert.Equal(fileInfo.DirectoryName, destinationFolder); Assert.Equal(fileInfo.FullName, destination); }