Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » File

Public general use code classes and xml files that we've compiled and used over the years:

File manipulation related support class.

   1:  using System;
   2:  using System.Web;
   3:  using System.Text;
   4:  using System.Text.RegularExpressions;
   5:  using System.IO;
   6:  using System.Configuration;
   7:  using System.Data;
   8:   
   9:  #if WINDOWS_FORM
  10:  using System.Windows.Forms;
  11:  #endif
  12:   
  13:  namespace Ia.Cl.Model
  14:  {
  15:      ////////////////////////////////////////////////////////////////////////////
  16:   
  17:      /// <summary publish="true">
  18:      /// File manipulation related support class.
  19:      /// 
  20:      /// </summary>
  21:      /// <value>
  22:      /// Unify path detection and handling into a single private function
  23:      /// </value>
  24:      /// <remarks> 
  25:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  26:      ///
  27:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  28:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  29:      ///
  30:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  31:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  32:      /// 
  33:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  34:      /// 
  35:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  36:      /// </remarks> 
  37:      public class File
  38:      {
  39:          ////////////////////////////////////////////////////////////////////////////
  40:   
  41:          /// <summary>
  42:          ///
  43:          /// </summary>
  44:          public File() { }
  45:   
  46:          ////////////////////////////////////////////////////////////////////////////
  47:   
  48:          /// <summary>
  49:          ///
  50:          /// </summary>
  51:          public static string Read(string file)
  52:          {
  53:              return Read(file, false, System.Text.Encoding.UTF8);
  54:          }
  55:   
  56:          ////////////////////////////////////////////////////////////////////////////
  57:   
  58:          /// <summary>
  59:          ///
  60:          /// </summary>
  61:          public static string Read(string file, System.Text.Encoding encoding)
  62:          {
  63:              return Read(file, false, encoding);
  64:          }
  65:   
  66:          ////////////////////////////////////////////////////////////////////////////
  67:   
  68:          /// <summary>
  69:          ///
  70:          /// </summary>
  71:          public static string Read(string absoluteOrRelativePathFile, bool useTemporaryFolder, System.Text.Encoding encoding)
  72:          {
  73:              // read text from file
  74:              string absolutePath;
  75:              string sa;
  76:              StringBuilder sb = new StringBuilder(100000);
  77:              StreamReader sr = null;
  78:   
  79:              absolutePath = null;
  80:   
  81:              // this will detect the initial "C:\" in the path_file to decide if it is absolute or relative
  82:              if (absoluteOrRelativePathFile.Contains(@":\"))
  83:              {
  84:                  // absolute path file
  85:                  absolutePath = absoluteOrRelativePathFile;
  86:              }
  87:              else
  88:              {
  89:                  absolutePath = global::Ia.Cl.Model.Default.AbsolutePath(useTemporaryFolder);
  90:   
  91:                  absolutePath += absoluteOrRelativePathFile;
  92:              }
  93:   
  94:              if (System.IO.File.Exists(absolutePath))
  95:              {
  96:                  using (sr = new StreamReader(absolutePath, encoding))
  97:                  {
  98:                      while ((sa = sr.ReadLine()) != null) sb.Append(sa + "\r\n");
  99:                  }
 100:              }
 101:              else { }
 102:   
 103:              return sb.ToString();
 104:          }
 105:   
 106:          ////////////////////////////////////////////////////////////////////////////
 107:   
 108:          /// <summary>
 109:          ///
 110:          /// </summary>
 111:          public static bool Create(string file, string line)
 112:          {
 113:              return Create(file, line, false);
 114:          }
 115:   
 116:          ////////////////////////////////////////////////////////////////////////////
 117:   
 118:          /// <summary>
 119:          ///
 120:          /// </summary>
 121:          public static bool Create(string absoluteOrRelativePathFile, string line, bool useTemporaryFolder)
 122:          {
 123:              // create text to file
 124:              bool done = false;
 125:              string absolutePath;
 126:              StreamWriter sw; // = null;
 127:   
 128:              absolutePath = global::Ia.Cl.Model.Default.AbsolutePath(useTemporaryFolder);
 129:   
 130:              absolutePath += absoluteOrRelativePathFile;
 131:   
 132:              using (sw = new StreamWriter(absolutePath, false, Encoding.UTF8))
 133:              {
 134:                  sw.Write(line);
 135:                  done = true;
 136:              }
 137:   
 138:              /*
 139:              below: this does not product good UTF8 encoding
 140:        
 141:              using(sw = System.IO.File.CreateText(path))
 142:              {
 143:                sw.WriteLine(line);
 144:                done = true;
 145:              }    
 146:              */
 147:   
 148:              return done;
 149:          }
 150:   
 151:          ////////////////////////////////////////////////////////////////////////////
 152:   
 153:          /// <summary>
 154:          ///
 155:          /// </summary>
 156:          public static bool Write(string absoluteOrRelativePathFile, string line)
 157:          {
 158:              return Write(absoluteOrRelativePathFile, line, false);
 159:          }
 160:   
 161:          ////////////////////////////////////////////////////////////////////////////
 162:   
 163:          /// <summary>
 164:          ///
 165:          /// </summary>
 166:          public static bool Write(string absoluteOrRelativePathFile, string line, bool useTemporaryFolder)
 167:          {
 168:              // write text to and existing file
 169:   
 170:              bool b;
 171:              string absolutePath, directory;
 172:   
 173:              b = false;
 174:              absolutePath = null;
 175:   
 176:              try
 177:              {
 178:                  // this will detect the initial "C:\" in the path_file to decide if it is absolute or relative
 179:                  if (absoluteOrRelativePathFile.Contains(@":\"))
 180:                  {
 181:                      // absolute path file
 182:                      absolutePath = absoluteOrRelativePathFile;
 183:                  }
 184:                  else
 185:                  {
 186:                      absolutePath = global::Ia.Cl.Model.Default.AbsolutePath(useTemporaryFolder);
 187:   
 188:                      absolutePath += absoluteOrRelativePathFile;
 189:                  }
 190:   
 191:                  directory = absolutePath.Substring(0, absolutePath.LastIndexOf(@"\"));
 192:   
 193:                  if (!Directory.Exists(directory))
 194:                  {
 195:                      // create the directory it does not exist.
 196:                      Directory.CreateDirectory(directory);
 197:                  }
 198:   
 199:                  // remove the readonly attribute if file exists
 200:                  if (File.Exists(absolutePath))
 201:                  {
 202:                      FileAttributes attributes = System.IO.File.GetAttributes(absolutePath);
 203:   
 204:                      if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
 205:                      {
 206:                          attributes = attributes & ~FileAttributes.ReadOnly;
 207:                          System.IO.File.SetAttributes(absolutePath, attributes);
 208:                      }
 209:                  }
 210:   
 211:                  using (FileStream fs = new FileStream(absolutePath, FileMode.Create))
 212:                  {
 213:                      using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
 214:                      {
 215:                          sw.WriteLine(line);
 216:                          b = true;
 217:                      }
 218:                  }
 219:              }
 220:              catch (Exception)
 221:              {
 222:                  b = false;
 223:              }
 224:   
 225:              return b;
 226:          }
 227:   
 228:          ////////////////////////////////////////////////////////////////////////////
 229:   
 230:          /// <summary>
 231:          ///
 232:          /// </summary>
 233:          public static bool Append(string absoluteOrRelativePathFile, string line)
 234:          {
 235:              return Append(absoluteOrRelativePathFile, line, false);
 236:          }
 237:   
 238:          ////////////////////////////////////////////////////////////////////////////
 239:   
 240:          /// <summary>
 241:          ///
 242:          /// </summary>
 243:          public static bool Append(string absoluteOrRelativePathFile, string line, bool useTemporaryFolder)
 244:          {
 245:              // append text to file
 246:              bool done = false;
 247:              string absolutePath;
 248:              StreamWriter sw = null;
 249:   
 250:              absolutePath = global::Ia.Cl.Model.Default.AbsolutePath(useTemporaryFolder);
 251:   
 252:              absolutePath += absoluteOrRelativePathFile;
 253:   
 254:              if (System.IO.File.Exists(absolutePath))
 255:              {
 256:                  using (sw = System.IO.File.AppendText(absolutePath))
 257:                  {
 258:                      sw.WriteLine(line);
 259:                      done = true;
 260:                  }
 261:              }
 262:              else done = false;
 263:   
 264:              return done;
 265:          }
 266:   
 267:          ////////////////////////////////////////////////////////////////////////////
 268:   
 269:          /// <summary>
 270:          ///
 271:          /// </summary>
 272:          public static bool Delete(string absoluteOrRelativePathFile)
 273:          {
 274:              // delete file
 275:              bool done = false;
 276:              string absolutePath;
 277:   
 278:              // this will detect the initial "C:\" in the path_file to decide if it is absolute or relative
 279:              if (absoluteOrRelativePathFile.Contains(@":\"))
 280:              {
 281:                  // absolute path file
 282:                  absolutePath = absoluteOrRelativePathFile;
 283:              }
 284:              else
 285:              {
 286:                  // relative path file
 287:                  absolutePath = global::Ia.Cl.Model.Default.AbsolutePath() + absoluteOrRelativePathFile;
 288:              }
 289:   
 290:              if (System.IO.File.Exists(absolutePath))
 291:              {
 292:                  System.IO.File.Delete(absolutePath);
 293:                  done = true;
 294:              }
 295:              else done = false;
 296:   
 297:              return done;
 298:          }
 299:   
 300:          ////////////////////////////////////////////////////////////////////////////
 301:   
 302:          /// <summary>
 303:          ///
 304:          /// </summary>
 305:          public static bool Move(string absoluteOrRelativeSourceFileName, string absoluteOrRelativeDestinationFileName)
 306:          {
 307:              // move (rename) file
 308:              bool done;
 309:              string absoluteSourceFileName, absoluteDestinationFileName;
 310:   
 311:              // this will detect the initial "C:\" in the path_file to decide if it is absolute or relative
 312:              if (absoluteOrRelativeSourceFileName.Contains(@":\")) absoluteSourceFileName = absoluteOrRelativeSourceFileName;
 313:              else absoluteSourceFileName = global::Ia.Cl.Model.Default.AbsoluteTempPath() + absoluteOrRelativeSourceFileName;
 314:   
 315:              if (absoluteOrRelativeDestinationFileName.Contains(@":\")) absoluteDestinationFileName = absoluteOrRelativeDestinationFileName;
 316:              else absoluteDestinationFileName = global::Ia.Cl.Model.Default.AbsoluteTempPath() + absoluteOrRelativeDestinationFileName;
 317:   
 318:              if (System.IO.File.Exists(absoluteSourceFileName) && !System.IO.File.Exists(absoluteDestinationFileName))
 319:              {
 320:                  System.IO.File.Move(absoluteSourceFileName, absoluteDestinationFileName);
 321:                  done = true;
 322:              }
 323:              else done = false;
 324:   
 325:              return done;
 326:          }
 327:   
 328:          ////////////////////////////////////////////////////////////////////////////
 329:   
 330:          /// <summary>
 331:          ///
 332:          /// </summary>
 333:          public static bool Exists(string file)
 334:          {
 335:              return Exists(file, false);
 336:          }
 337:   
 338:          ////////////////////////////////////////////////////////////////////////////
 339:   
 340:          /// <summary>
 341:          ///
 342:          /// </summary>
 343:          public static bool Exists(string absoluteOrRelativePathFile, bool useTemporaryFolder)
 344:          {
 345:              // check if file exists
 346:              bool done = false;
 347:              string absolutePath;
 348:   
 349:              absolutePath = global::Ia.Cl.Model.Default.AbsolutePath(useTemporaryFolder);
 350:   
 351:              absolutePath += absoluteOrRelativePathFile;
 352:   
 353:              if (System.IO.File.Exists(absolutePath)) done = true;
 354:              else done = false;
 355:   
 356:              return done;
 357:          }
 358:   
 359:          ////////////////////////////////////////////////////////////////////////////
 360:   
 361:          /// <summary>
 362:          ///
 363:          /// </summary>
 364:          public static StreamReader OpenText(string file)
 365:          {
 366:              // 
 367:              string path;
 368:              StreamReader sr;
 369:   
 370:              sr = null;
 371:   
 372:              path = global::Ia.Cl.Model.Default.AbsolutePath();
 373:   
 374:              path = path + file;
 375:   
 376:              sr = System.IO.File.OpenText(path);
 377:   
 378:              return sr;
 379:          }
 380:   
 381:          ////////////////////////////////////////////////////////////////////////////
 382:   
 383:          /// <summary>
 384:          /// Count the number of files within path that conform to the regular expression passed in file
 385:          /// <param name="file">File name (could be a regular expression)</param>
 386:          /// <param name="path">Directory within which to search</param>
 387:          /// <returns>int value of number of occurances of file withing path</returns>
 388:          /// </summary>
 389:          public static int Count(string path, string file)
 390:          {
 391:              int c;
 392:              FileInfo[] fip;
 393:   
 394:              fip = Info(path, file);
 395:   
 396:              if (fip != null) c = fip.Length;
 397:              else c = 0;
 398:   
 399:              return c;
 400:          }
 401:   
 402:          ////////////////////////////////////////////////////////////////////////////
 403:   
 404:          /// <summary>
 405:          /// Return the files within path that conform to the regular expression passed in file
 406:          /// <param name="file">File name (could be a regular expression)</param>
 407:          /// <param name="path">Directory within which to search</param>
 408:          /// <returns>FileInfo[] of files within directory</returns>
 409:          /// </summary>
 410:          public static FileInfo[] Info(string path, string file)
 411:          {
 412:              string absolutePath;
 413:              FileInfo[] fip;
 414:              DirectoryInfo di;
 415:   
 416:              absolutePath = "";
 417:              fip = null;
 418:   
 419:              try
 420:              {
 421:  #if WINDOWS_FORM
 422:  #else
 423:                  absolutePath = AppDomain.CurrentDomain.BaseDirectory.ToString();
 424:  #endif
 425:                  absolutePath += path;
 426:   
 427:                  di = new DirectoryInfo(absolutePath);
 428:                  fip = di.GetFiles(file);
 429:              }
 430:              catch (Exception)
 431:              {
 432:                  //throw(Exception ERRORMAN); // result_l.Text += " The process failed: "+e.ToString();
 433:              }
 434:   
 435:              return fip;
 436:          }
 437:   
 438:          ////////////////////////////////////////////////////////////////////////////
 439:   
 440:          /// <summary>
 441:          /// 
 442:          /// </summary>
 443:          public static bool IsValidFilename(string testName)
 444:          {
 445:              bool b;
 446:   
 447:              Regex containsABadCharacter = new Regex("[" + Regex.Escape(System.IO.Path.GetInvalidFileNameChars().ToString()) + "]");
 448:   
 449:              if (containsABadCharacter.IsMatch(testName)) b = false;
 450:              else b = true;
 451:   
 452:              return b;
 453:          }
 454:   
 455:          ////////////////////////////////////////////////////////////////////////////
 456:          ////////////////////////////////////////////////////////////////////////////
 457:      }
 458:  }