C:\> Rostislav Persion's Projects

.:: Search-n-run ::.
Looks for new files and opens them






This is a program written in C# that monitors a folder for specified file extensions, and opens them when they are created.

SOFTWARE WRITTEN IN C#

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  using System.IO;
   7:  using System.Collections;
   8:   
   9:  using System.Threading;
  10:   
  11:  namespace search_n_run_02
  12:  {
  13:      class Program
  14:      {
  15:          static void Main(string[] args)
  16:          {
  17:   
  18:              // SET BACKGROUND COLOR
  19:              Console.BackgroundColor = ConsoleColor.DarkBlue;
  20:              Console.Clear();
  21:   
  22:              // WELCOME MESSAGE
  23:              Console.ForegroundColor = ConsoleColor.Yellow;
  24:              Console.WriteLine("#######   WELCOME TO SEARCH-N-RUN 2.00    #######");
  25:              Console.WriteLine("####### CREATED BY ROSTISLAV PERSION 2015 #######");
  26:              Console.WriteLine("");
  27:   
  28:   
  29:   
  30:              // LOAD INI SETTINGS
  31:              string targetDirectory = "";
  32:              int sleepTime = 0;
  33:              string runCommand = "";
  34:              string[] fileExt = {};
  35:   
  36:              try
  37:              {
  38:                  // read from file
  39:                  System.IO.StreamReader file = new System.IO.StreamReader("args.ini");
  40:                  string line1 = file.ReadLine();
  41:                  string line2 = file.ReadLine();
  42:                  string line3 = file.ReadLine();
  43:                  string line4 = file.ReadLine();
  44:                  file.Close();
  45:   
  46:                  // init vars
  47:                  targetDirectory = line1.Trim();
  48:                  Console.ForegroundColor = ConsoleColor.White;
  49:                  Console.WriteLine("TARGET DIRECTORY: " + targetDirectory);
  50:   
  51:                  sleepTime = Convert.ToInt32(line2.Trim());
  52:                  Console.ForegroundColor = ConsoleColor.White;
  53:                  Console.WriteLine("LOOP DELAY: " + sleepTime.ToString().Trim() + " mS");
  54:   
  55:                  runCommand = line3.Trim();
  56:                  Console.ForegroundColor = ConsoleColor.White;
  57:                  Console.WriteLine("CONSOLE COMMAND: " + runCommand);
  58:   
  59:                  fileExt = line4.Split (',');
  60:                  Console.ForegroundColor = ConsoleColor.White;
  61:                  Console.Write("FILE EXTENSIONS: ");
  62:                  for (int i = 0; i < fileExt.Length; i++)
  63:                  {
  64:                      Console.Write(fileExt[i].ToUpper().Trim() + " ");
  65:                  }
  66:                  Console.WriteLine();
  67:   
  68:                 
  69:   
  70:              }
  71:              catch (Exception e)
  72:              {
  73:                  Console.ForegroundColor = ConsoleColor.Red;
  74:                  Console.WriteLine("PROBLEM LOADING SETTINGS!");
  75:                  Console.ReadLine();
  76:                  Environment.Exit(0);
  77:              }
  78:   
  79:   
  80:   
  81:              // LOOK FOR NEW FILES
  82:              Console.ForegroundColor = ConsoleColor.Yellow;
  83:              Console.WriteLine("");
  84:              Console.WriteLine("LOOKING FOR NEW FILES....");
  85:              Console.WriteLine("");
  86:              Console.ForegroundColor = ConsoleColor.White;
  87:   
  88:              DateTime startTime = DateTime.Now;
  89:   
  90:              while (true)
  91:              {
  92:   
  93:                  string[] fileEntries = { };
  94:   
  95:                  try
  96:                  {
  97:                      fileEntries = Directory.GetFiles(targetDirectory);
  98:                  }
  99:                  catch(Exception e)
 100:                  {
 101:                      Console.ForegroundColor = ConsoleColor.Red;
 102:                      Console.WriteLine("PROBLEM READING FOLDER!");
 103:                      Console.ReadLine();
 104:                      Environment.Exit(0);
 105:                  }
 106:   
 107:   
 108:   
 109:                  foreach (string fileName in fileEntries)
 110:                  {
 111:   
 112:                      //IF FILE PATTERN THEN CONTINUE
 113:                      string[] fileParts = fileName.Split('.');
 114:                      string ext = fileParts[fileParts.Length - 1].ToUpper().Trim();
 115:   
 116:                      bool isExt = false;
 117:                      for (int i = 0; i < fileExt.Length; i++)
 118:                      {
 119:                          if (ext.Trim().ToUpper() == fileExt[i].Trim().ToUpper())
 120:                          {
 121:                              isExt = true;
 122:                              break;
 123:                          }
 124:                      }
 125:                      if(isExt == true)
 126:                      {
 127:                          DateTime creationTime = File.GetCreationTime(fileName);
 128:                          DateTime writeTime = File.GetLastWriteTime(fileName);
 129:   
 130:                          if (creationTime < writeTime) { creationTime = writeTime; }
 131:   
 132:                          if (startTime < creationTime)
 133:                          {
 134:                              Console.WriteLine("NEW FILE: " + fileName);
 135:                              System.Diagnostics.Process.Start(runCommand, fileName);
 136:                          }
 137:                      }
 138:   
 139:   
 140:                  }
 141:   
 142:                  startTime = DateTime.Now;
 143:                  Thread.Sleep(sleepTime);
 144:              }
 145:   
 146:   
 147:   
 148:          }
 149:      }
 150:  }