| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- //
- // System.CodeDom.Compiler TempFileCollection Class implementation
- //
- // Author:
- // Dick Porter ([email protected])
- //
- // (C) 2003 Ximian, Inc.
- //
- using System.IO;
- using System.Collections;
- namespace System.CodeDom.Compiler
- {
- public class TempFileCollection:ICollection, IEnumerable, IDisposable
- {
- Hashtable filehash;
- string tempdir;
- bool keepfiles;
-
- public TempFileCollection(): this(null, false)
- {
- }
- public TempFileCollection(string tempDir): this(tempDir, false)
- {
- }
- public TempFileCollection(string tempDir, bool keepFiles)
- {
- filehash=new Hashtable();
- tempdir=tempDir;
- keepfiles=keepFiles;
- }
- private string basepath=null;
-
- public string BasePath
- {
- get {
- if(basepath==null) {
- if(tempdir==null) {
- /* Get the system temp dir */
- MonoIO.GetTempPath(out tempdir);
- }
- string random=new Random().Next(10000,99999).ToString();
-
- if(tempdir.EndsWith("\\") ||
- tempdir.EndsWith("/")) {
- basepath=tempdir+random;
- } else {
- basepath=tempdir+"/"+random;
- }
- }
- return(basepath);
- }
- }
- public int Count
- {
- get {
- return(filehash.Count);
- }
- }
- public bool KeepFiles
- {
- get {
- return(keepfiles);
- }
- set {
- keepfiles=value;
- }
- }
- public string TempDir
- {
- get {
- if(tempdir==null) {
- return(String.Empty);
- } else {
- return(tempdir);
- }
- }
- }
- public string AddExtension(string fileExtension)
- {
- return(AddExtension(fileExtension, keepfiles));
- }
- public string AddExtension(string fileExtension, bool keepFile)
- {
- string filename=BasePath+"."+fileExtension;
- AddFile(filename, keepFile);
- return(filename);
- }
- public void AddFile(string fileName, bool keepFile)
- {
- filehash.Add(fileName, keepFile);
- }
- public void CopyTo(string[] fileNames, int start)
- {
- filehash.Keys.CopyTo(fileNames, start);
- }
- void ICollection.CopyTo(Array array, int start)
- {
- filehash.Keys.CopyTo(array, start);
- }
- object ICollection.SyncRoot {
- get {
- return filehash.SyncRoot;
- }
- }
- bool ICollection.IsSynchronized {
- get {
- return(false);
- }
- }
-
- void IDisposable.Dispose()
- {
- Dispose(true);
- }
-
- public void Delete()
- {
- string[] filenames=new string[filehash.Count];
- filehash.Keys.CopyTo(filenames, 0);
- foreach(string file in filenames) {
- if((bool)filehash[file]==true) {
- File.Delete(file);
- filehash.Remove(file);
- }
- }
- }
- public IEnumerator GetEnumerator()
- {
- return(filehash.Keys.GetEnumerator());
- }
- protected virtual void Dispose(bool disposing)
- {
- Delete();
- }
- ~TempFileCollection()
- {
- Dispose(false);
- }
-
- }
- }
|