clsTempFile.cs

00001 // C# ClassLib
00002 // http://csharp-classlib.sourceforge.net
00003 //
00004 //
00005 // Copyright (C) 2005 Marcel Joachim Kloubert
00006 // 
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 // 
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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                                 // temp directory was not found!
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                                 // deletes the file if still exists
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                                         // init random numbers & string buffer
00202                                         strBuffer = "";                 
00203                         
00204                                         // create first part
00205                                         for (i = 0; i < 8; i++)
00206                                                 strBuffer += cstrAlpha.Substring( this.m_oRandom.Next(cstrAlpha.Length), 1 );
00207                         
00208                                         // add dot
00209                                         strBuffer += ".";
00210                         
00211                                         // create extension
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                                                 // return generated filename only if not yet exists
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 }

Generated on Thu Jun 2 08:41:49 2005 for C# ClassLib by  doxygen 1.4.1