شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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