/Code/Foundation/TempFile.cs

https://github.com/DavidMoore/Foundation · C# · 125 lines · 65 code · 13 blank · 47 comment · 5 complexity · eb9d04bc1467f9a78323e50e962696da MD5 · raw file

  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using Foundation.Windows;
  7. namespace Foundation
  8. {
  9. /// <summary>
  10. /// Automatically creates a temporary file in the system's
  11. /// temporary directory, removing the file from the system when it
  12. /// goes out of scope.
  13. /// </summary>
  14. /// <example>
  15. /// string filename;
  16. /// using(TempFile file = new TempFile())
  17. /// {
  18. /// filename = file.FileInfo.FullName;
  19. /// Console.WriteLine("About to write to temporary file: {0}", filename);
  20. /// file.WriteAllText("This is some test text");
  21. /// }
  22. /// if( !File.Exists(filename ) MessageWindow.Show("Temp file was deleted!");
  23. /// </example>
  24. public class TempFile : IDisposable
  25. {
  26. readonly FileInfo fileInfo;
  27. /// <summary>
  28. /// Creates a new temporary file, which ensures the temporary
  29. /// file is deleted once the object is disposed.
  30. /// </summary>
  31. public TempFile()
  32. {
  33. fileInfo = new FileInfo(Path.GetTempFileName());
  34. }
  35. /// <summary>
  36. /// Creates a temporary file, using the passed format string to
  37. /// create the name. Parameter 0 is the generated temporary filename.
  38. /// e.g. to create a temp file with a .jpg extension, the passed
  39. /// name should be "{0}.jpg"
  40. /// </summary>
  41. /// <param name="name">The string format for the name, taking {0} as the generated temp file name</param>
  42. public TempFile(string name) : this()
  43. {
  44. // Strip the extension
  45. var nameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.Name);
  46. // Get the new name
  47. var newName = string.Format(CultureInfo.CurrentCulture, name, nameWithoutExtension);
  48. fileInfo.MoveTo( Path.Combine(fileInfo.DirectoryName, newName ) );
  49. }
  50. /// <summary>
  51. /// Handle to the temporary file.
  52. /// </summary>
  53. public FileInfo FileInfo { get { return fileInfo; } }
  54. ///<summary>
  55. /// Deletes the temporary file once out of scope or disposed.
  56. ///</summary>
  57. ///<filterpriority>2</filterpriority>
  58. public void Dispose()
  59. {
  60. Dispose(true);
  61. GC.SuppressFinalize(this);
  62. }
  63. /// <summary>
  64. /// Frees up managed resources.
  65. /// </summary>
  66. /// <param name="disposing"></param>
  67. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  68. void Dispose(bool disposing)
  69. {
  70. if( !disposing || fileInfo == null ) return;
  71. // If the file doesn't exist, we don't have to do anything.
  72. fileInfo.Refresh();
  73. if(!fileInfo.Exists ) return;
  74. try
  75. {
  76. fileInfo.Delete();
  77. }
  78. catch(IOException ioe)
  79. {
  80. try
  81. {
  82. // If we can't delete the temp file now (likely because it's locked for some reason),
  83. // we can schedule to delete it on reboot when the handles on it should be gone. We need to
  84. // be an administrator to do this.
  85. if (Win32Api.IO.MoveFileEx(fileInfo.FullName, null, Win32Api.IO.MoveFileFlags.DelayUntilReboot)) return;
  86. throw new FoundationException("Couldn't schedule delete of locked file '{0}' at reboot.",
  87. Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()));
  88. }
  89. catch (Exception)
  90. {
  91. FoundationEventLog.Error(ioe, "Couldn't delete temporary file '{0}'.", fileInfo);
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. FoundationEventLog.Error(ex, "Couldn't delete temporary file '{0}'.", fileInfo);
  97. }
  98. }
  99. /// <summary>Opens a text file, reads all lines of the file, and then closes the file.</summary>
  100. /// <returns>The contents of the file</returns>
  101. public string ReadAllText()
  102. {
  103. return File.ReadAllText(fileInfo.FullName);
  104. }
  105. /// <summary>Creates a new file, writes the specified string array to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.</summary>
  106. /// <param name="contents"></param>
  107. public void WriteAllText(string contents)
  108. {
  109. File.WriteAllText(fileInfo.FullName, contents);
  110. }
  111. }
  112. }