TempFileCollection.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // System.CodeDom.Compiler TempFileCollection Class implementation
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Copyright 2003 Ximian, Inc.
  8. //
  9. using System.IO;
  10. using System.Collections;
  11. namespace System.CodeDom.Compiler
  12. {
  13. public class TempFileCollection:ICollection, IEnumerable, IDisposable
  14. {
  15. Hashtable filehash;
  16. string tempdir;
  17. bool keepfiles;
  18. string basepath;
  19. Random rnd;
  20. public TempFileCollection(): this(null, false)
  21. {
  22. }
  23. public TempFileCollection(string tempDir): this(tempDir, false)
  24. {
  25. }
  26. public TempFileCollection(string tempDir, bool keepFiles)
  27. {
  28. filehash=new Hashtable();
  29. tempdir=tempDir;
  30. keepfiles=keepFiles;
  31. }
  32. public string BasePath
  33. {
  34. get {
  35. if(basepath==null) {
  36. if (tempdir==null) {
  37. /* Get the system temp dir */
  38. MonoIO.GetTempPath(out tempdir);
  39. }
  40. if (rnd == null)
  41. rnd = new Random ();
  42. string random = rnd.Next (10000,99999).ToString ();
  43. basepath = Path.Combine (tempdir, random);
  44. }
  45. return(basepath);
  46. }
  47. }
  48. public int Count
  49. {
  50. get {
  51. return(filehash.Count);
  52. }
  53. }
  54. public bool KeepFiles
  55. {
  56. get {
  57. return(keepfiles);
  58. }
  59. set {
  60. keepfiles=value;
  61. }
  62. }
  63. public string TempDir
  64. {
  65. get {
  66. if(tempdir==null) {
  67. return(String.Empty);
  68. } else {
  69. return(tempdir);
  70. }
  71. }
  72. }
  73. public string AddExtension(string fileExtension)
  74. {
  75. return(AddExtension(fileExtension, keepfiles));
  76. }
  77. public string AddExtension(string fileExtension, bool keepFile)
  78. {
  79. string filename=BasePath+"."+fileExtension;
  80. AddFile(filename, keepFile);
  81. return(filename);
  82. }
  83. public void AddFile(string fileName, bool keepFile)
  84. {
  85. filehash.Add(fileName, keepFile);
  86. }
  87. public void CopyTo(string[] fileNames, int start)
  88. {
  89. filehash.Keys.CopyTo(fileNames, start);
  90. }
  91. void ICollection.CopyTo(Array array, int start)
  92. {
  93. filehash.Keys.CopyTo(array, start);
  94. }
  95. object ICollection.SyncRoot {
  96. get {
  97. return filehash.SyncRoot;
  98. }
  99. }
  100. bool ICollection.IsSynchronized {
  101. get {
  102. return(false);
  103. }
  104. }
  105. void IDisposable.Dispose()
  106. {
  107. Dispose(true);
  108. }
  109. public void Delete()
  110. {
  111. string[] filenames=new string[filehash.Count];
  112. filehash.Keys.CopyTo(filenames, 0);
  113. foreach(string file in filenames) {
  114. if((bool)filehash[file]==false) {
  115. File.Delete(file);
  116. filehash.Remove(file);
  117. }
  118. }
  119. }
  120. public IEnumerator GetEnumerator()
  121. {
  122. return(filehash.Keys.GetEnumerator());
  123. }
  124. protected virtual void Dispose(bool disposing)
  125. {
  126. Delete();
  127. }
  128. ~TempFileCollection()
  129. {
  130. Dispose(false);
  131. }
  132. }
  133. }