Sunday, September 8, 2013

C# - Search Application in 10 minutes...

Quick Search application
This paragraph will demonstrate a simple application that running search on system objects, I created this app in 10 minutes so if you want to add your comments you more than welcome.

Application Summary:
       -          Ability to search files via file extension.
        -          Ability to search files via file name.
       -          User file validator using the path class.
       -          Try and Catch mechanism to handle the application failures.

Folder I used for this code:

















Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            bool Exit_Criteria = true;
            do
            {
                Console.WriteLine("Please insert file name including extension:");
                string userfile = Console.ReadLine();
                Console.WriteLine();
                Console.WriteLine("Please insert Path for Search:");
                string path = Console.ReadLine();
                Console.WriteLine();
                int result = Validator(userfile, path);
                //result values :
                /*
                 0 = Validation Failed on File Name no need to validate folder location.
                 1 = Validation passed for file name but faied for path.
                 2 = Validation approved.                  
                 */
                switch (result)
                {
                    case 0:
                        Console.WriteLine("Please insert a valid file name\n");
                        break;
                    case 1:
                        Console.WriteLine("Please insert a valid path\n");
                        break;
                    case 2:
                        Exit_Criteria = Menu(Exit_Criteria, userfile, path);
                        break;
                }
            } while (Exit_Criteria == true);
        }
        private static bool Menu(bool Exit_Criteria, string userfile, string path)
        {
            int counter = 0;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("File is valid for search\n");
            Console.ResetColor();                                                        
            Console.WriteLine("[1] = Search by extension");
            Console.WriteLine("[2] = Search by file name");
            Console.WriteLine("[3] = Exit\n");
            int userselection = int.Parse(Console.ReadLine());
            string[] PathFiles = Directory.GetFiles(path);
            switch (userselection)
            {
                case 1:
                    string extention = Path.GetExtension(userfile);
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Avilable Files in folder:");
                    Console.ResetColor();
                    foreach (var FileExte in PathFiles)
                    {
                        if (extention == Path.GetExtension(FileExte))
                        {
                            Console.WriteLine(FileExte);
                             counter++;
                        }
                    }
                    if (counter == 0)
                    {
                        Console.WriteLine("None\n");
                    }
                    else
                    {
                        counter = 0;
                    }
                    Console.WriteLine();
                    break;
                case 2:
                    string FileNameWithoutExtension = Path.GetFileNameWithoutExtension(userfile);
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Avilable Files in folder:");
                    Console.ResetColor();
                    foreach (var Fname in PathFiles)
                    {
                        if (FileNameWithoutExtension == Path.GetFileNameWithoutExtension(Fname))
                        {
                            Console.WriteLine(Fname);
                            counter++;
                        }
                    }
                    if (counter == 0)
                    {
                        Console.WriteLine("File not found in scope\n");
                    }
                    else
                    {
                        counter = 0;
                    }
                    break;
                case 3:
                    Exit_Criteria = false;
                    break;
            }
            return Exit_Criteria;
        }
        private static int Validator(string TmpFile, string PathLocation)
        {
            int result = 0;
            if (Path.GetExtension(TmpFile) != "" && Path.GetFileNameWithoutExtension(TmpFile) != "") //Validation user file                             
            {
                result++;   //1
            }
            else
            {
                return result;  //0
            }
            if (Directory.Exists(PathLocation) == true)
            {
                result++;  //2
            }
            else
            {
                return result; //1
            }
            return result;//2
        }
    }
}



Extension search:
File name search:



No comments:

Post a Comment

My Presentations