00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 using System;
00023 using System.IO;
00024
00025 namespace CSharp_ClassLib.Classes
00026 {
00049 public class TempFile : System.Object
00050 {
00052 private string m_strTMPDirectory;
00054 private System.Random m_oRandom;
00056 private System.IO.FileStream m_oFileStream;
00057
00070 public TempFile()
00071 {
00072 m_strTMPDirectory = "./";
00073
00074 this.InitRest();
00075
00076 return;
00077 }
00078
00095 public TempFile(string Temp_Directory)
00096 {
00097 if (!System.IO.Directory.Exists(Temp_Directory))
00098
00099 throw ( new System.IO.DirectoryNotFoundException("The directory \"" + Temp_Directory + "\" was not found!") );
00100
00101 m_strTMPDirectory = this.TempDirectory;
00102
00103 this.InitRest();
00104
00105 return;
00106 }
00107
00120 ~TempFile()
00121 {
00122 string strFilename = this.Filestream.Name;
00123
00124 this.Filestream.Close();
00125
00126 if ( System.IO.File.Exists(strFilename) )
00127
00128 System.IO.File.Delete(strFilename);
00129
00130 return;
00131 }
00132
00137 private void InitRest ()
00138 {
00139 m_oRandom = new System.Random( this.LongToInt(System.DateTime.Now.Ticks) );
00140
00141 m_oFileStream = new System.IO.FileStream(this.TempDirectory + "/" + this.UniqueFilename, System.IO.FileMode.CreateNew, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read );
00142
00143 System.IO.File.SetAttributes(this.Filestream.Name, System.IO.FileAttributes.Hidden);
00144
00145 return;
00146 }
00147
00162 public System.IO.FileStream Filestream
00163 {
00164 get { return m_oFileStream; }
00165 }
00166
00181 public string TempDirectory { get { return m_strTMPDirectory; } }
00182
00183
00190 private string UniqueFilename
00191 {
00192 get
00193 {
00194 string strBuffer = "";
00195 int i = 0;
00196
00197 const string cstrAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
00198
00199 while (true)
00200 {
00201
00202 strBuffer = "";
00203
00204
00205 for (i = 0; i < 8; i++)
00206 strBuffer += cstrAlpha.Substring( this.m_oRandom.Next(cstrAlpha.Length), 1 );
00207
00208
00209 strBuffer += ".";
00210
00211
00212 for (i = 0; i < 3; i++)
00213 strBuffer += cstrAlpha.Substring( this.m_oRandom.Next(cstrAlpha.Length), 1 );
00214
00215 if ( !System.IO.File.Exists(this.m_strTMPDirectory + "/" + strBuffer) )
00216
00217 return strBuffer;
00218 }
00219 }
00220 }
00221
00232 private int LongToInt (long Input)
00233 {
00234 if ( Input > int.MaxValue )
00235 return int.MaxValue;
00236 else if ( Input < int.MinValue )
00237 return int.MinValue;
00238 else
00239 return (int)Input;
00240 }
00241 }
00242 }