TempFileCollection.cs 2.6 KB

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