Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Source Code Super Search

Rate me:
Please Sign up or sign in to vote.
4.71/5 (22 votes)
10 Mar 2009CPOL8 min read 52.3K   1.5K   66  
A simple solution for searching source code directories
// BaileySoft Super Code Search
// Copyright (c) 2008, BSLTD
// All rights reserved.
//
// NOTES: This class was removed from the application but remains
//        in the source files for reference. 
//
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace BSCodeGrep
{
    public enum ExecutionType
    {
        WINDOWS_EXECUTE,
        SHELL_EXECUTE_HIDDEN,
        SHELL_EXECUTE_VISIBLE
    }

    public class CommandLine
    {
        private string m_shellOutput;
        private Exception m_Error;
        private ExecutionType m_ExType;

        private string m_ApplicationPath;
        private string m_ApplicationParameters;
        private int? m_TimeOutThreshold;

        /// <summary>
        /// The output displayed in the com shell
        /// </summary>
        public String ShellOutput
        {
            get
            {
                return m_shellOutput;
            }
        }

        /// <summary>
        /// The Error thrown by the entity
        /// </summary>
        public Exception Error
        {
            get
            {
                return m_Error ?? new SystemException(); 
            }
        }

        /// <summary>
        /// The type of execution specified by the caller
        /// </summary>
        public ExecutionType ExecuteType
        {
            get
            {
                return m_ExType;
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="exType">ExecutionType for the process</param>
        public CommandLine(ExecutionType exType)
        {
            this.m_ExType = exType;
        }

        /// <summary>
        /// Executes the process
        /// </summary>
        /// <param name="applicationPath">The path to the process to start</param>
        /// <param name="parameters">The parameters to send to the process</param>
        /// <param name="timeout">Optional int timeout value</param>
        public void ExecuteCommand(string applicationPath, string parameters, int? timeout)
        {
            m_ApplicationPath = applicationPath;
            m_ApplicationParameters = parameters;
            m_TimeOutThreshold = timeout;

            switch (ExecuteType)
            {
                case ExecutionType.WINDOWS_EXECUTE:
                    ExecNoWindow();
                    break;
                case ExecutionType.SHELL_EXECUTE_VISIBLE:
                    ExecCommandPrompt();
                    break;
                case ExecutionType.SHELL_EXECUTE_HIDDEN:
                    ExecCommandPromptHidden();
                    break;
            }
        }

        private void ExecNoWindow()
        {
            ProcessStartInfo psi = new ProcessStartInfo(m_ApplicationPath, m_ApplicationParameters);
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.CreateNoWindow = true;

            Process p_exec = new Process();
            StreamReader p_output = null;

            try
            {
                p_exec = Process.Start(psi);
                p_output = p_exec.StandardOutput;

                if (m_TimeOutThreshold != null)
                {
                    if (m_TimeOutThreshold == 0)
                    {
                        p_exec.WaitForExit();
                    }
                    else
                    {
                        p_exec.WaitForExit((int)m_TimeOutThreshold);
                    }
                }

                if (p_exec.HasExited)
                {
                    m_shellOutput = p_output.ReadToEnd();
                }
            }
            catch (SystemException e)
            {
                m_Error = e;
            }
            finally
            {
                p_exec.Close();
                p_output.Close();
            }            
        }

        private void ExecCommandPrompt()
        {
            DoShellExecute();
        }

        private void ExecCommandPromptHidden()
        {
            DoShellExecute();
        }

        private void DoShellExecute()
        {
            ProcessStartInfo psi = new ProcessStartInfo(m_ApplicationPath, m_ApplicationParameters);
            psi.UseShellExecute = true;
            psi.RedirectStandardOutput = false;
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Normal;

            if (ExecuteType == ExecutionType.SHELL_EXECUTE_HIDDEN)
                psi.WindowStyle = ProcessWindowStyle.Hidden;

            Process p_exec = new Process();

            try
            {
                p_exec = Process.Start(psi);

                if (m_TimeOutThreshold != null || m_TimeOutThreshold == 0)
                {
                    p_exec.WaitForExit((int)m_TimeOutThreshold);
                }
                else
                {
                    p_exec.WaitForExit();
                }
            }
            catch (SystemException e)
            {
                m_Error = e;
            }
            finally
            {
                p_exec.Close();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I'm a professional .NET software developer and proud military veteran. I've been in the software business for 20+ years now and if there's one lesson I have learned over the years, its that in this industry you have to be prepared to be humbled from time to time and never stop learning!

Comments and Discussions