/// <summary> /// Opens a text file, /// tries reading file 500 times before throwing IO Exception, /// and then closes the file. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <returns>Task which ultimately returns a string containing all lines of the file.</returns> public async static Task <string> ReadAllTextRetry(string fileName) { if (string.IsNullOrEmpty(fileName)) { return(null); } // If the file doesn't exists we don't need to try 500 time to open it. if (!File.Exists(fileName)) { return(null); } int retryCount = 500; try { return(await PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), retryCount) .ExecuteAsync(() => Task.FromResult <string>(File.ReadAllText(fileName)))); } catch (IOException) { Logger.Log("Exception: Tried " + retryCount + " times for reading, but the file " + fileName + " is still in use. Exiting gracefully."); } return(string.Empty); }
/// <summary> /// Creates a new file, writes the specified byte array to the file, and then closes /// the file. If the target file already exists, it is overwritten. If the target /// file is in use, try 5 times before throwing IO Exception. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <param name="value">The bytes to write to the file.</param> public async static Task WriteAllBytesRetry(string fileName, byte[] value) { int retryCount = 500; try { await Task.Run(() => File.WriteAllBytes(fileName, value)) .ExecuteRetryableTaskAsync(PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), retryCount)); } catch (IOException) { Logger.Log("Exception: Tried" + retryCount + " times for writing, but the file " + fileName + " is still in use. Exiting gracefully."); } }
/// <summary> /// Creates a new file, writes the specified string to the file, and then closes /// the file. If the target file already exists, it is overwritten. If the target /// file is in use, try 5 times before throwing IO Exception. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <param name="contents">The string to write to the file.</param> public async static Task WriteAllTextRetry(string fileName, string contents, bool withBOM = true) { int retryCount = 500; try { await Task.Run(() => File.WriteAllText(fileName, contents, new UTF8Encoding(withBOM))) .ExecuteRetryableTaskAsync(PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), retryCount)); } catch (IOException) { Logger.Log("Exception: Tried " + retryCount + " times for writing, but the file " + fileName + " is still in use. Exiting gracefully."); } }
/// <summary> /// Opens a text file, /// tries reading file into a byte array 5 times before throwing IO Exception, /// and then closes the file. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <returns>Task which ultimately returns all lines of the file, or the lines that are the result of a query.</returns> public async static Task <byte[]> ReadAllBytesRetry(string fileName) { int retryCount = 500; try { return(await Task.FromResult <byte[]>(File.ReadAllBytes(fileName)) .ExecuteRetryableTaskAsync <byte[]>(PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), retryCount))); } catch (IOException) { Logger.Log("Exception: Tried " + retryCount + " times for reading, but the file " + fileName + " is still in use. Exiting gracefully."); } return(Enumerable.Empty <byte>().ToArray()); }
/// <summary> /// Tries reading the lines of a file 500 times before throwing IO Exception. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <returns>Task which ultimately returns all lines of the file, or the lines that are the result of a query.</returns> public async static Task <IEnumerable <string> > ReadAllLinesRetry(string fileName) { if (string.IsNullOrEmpty(fileName)) { return(null); } int retryCount = 500; try { return(await PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), retryCount) .ExecuteAsync(() => Task.FromResult <IEnumerable <string> >(File.ReadLines(fileName)))); } catch (IOException) { Logger.Log("Exception: Tried " + retryCount + " times for reading, but the file " + fileName + " is still in use. Exiting gracefully."); } return(Enumerable.Empty <string>()); }
/// <summary> /// Creates a new file, writes the specified byte array to the file, and then closes /// the file. If the target file already exists, it is overwritten. If the target /// file is in use, try 5 times before throwing IO Exception. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <param name="value">The bytes to write to the file.</param> public async static Task WriteAllBytesRetry(string fileName, byte[] value) { await BeginWrite(Task.Run(() => File.WriteAllBytes(fileName, value)) .ExecuteRetryableTaskAsync(PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), 5))); }
/// <summary> /// Creates a new file, writes the specified string to the file, and then closes /// the file. If the target file already exists, it is overwritten. If the target /// file is in use, try 5 times before throwing IO Exception. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <param name="contents">The string to write to the file.</param> public async static Task WriteAllTextRetry(string fileName, string contents) { await BeginWrite(Task.Run(() => File.WriteAllText(fileName, contents, Encoding.UTF8)) .ExecuteRetryableTaskAsync(PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), 5))); }
/// <summary> /// Opens a text file, /// tries reading file into a byte array 5 times before throwing IO Exception, /// and then closes the file. /// </summary> /// <param name="fileName">The file to open for reading.</param> /// <returns>Task which ultimately returns all lines of the file, or the lines that are the result of a query.</returns> public async static Task <byte[]> ReadAllBytesRetry(string fileName) { return(await BeginRead <byte[]>(Task.FromResult <byte[]>(File.ReadAllBytes(fileName)) .ExecuteRetryableTaskAsync <byte[]>(PolicyFactory.GetPolicy(new FileTransientErrorDetectionStrategy(), 5)))); }