Click here to Skip to main content
12,300,607 members (23,193 online)
Click here to Skip to main content

Stats

33.5K views
1K downloads
65 bookmarked
Posted

Source Code Super Search

, 10 Mar 2009 CPOL
A simple solution for searching source code directories
BSCodeGrep
branches
0.9 VS Add-In
BSCodeGrep
Classes
folder_explore copy.ico
Forms
icons
add.png
application_form.png
arrow_right.png
delete.png
find.png
folder.png
folder_explore copy.ico
folder_explore.png
folder_page.png
information.png
lightning.png
magnifier.png
note_edit.png
package_system.png
star.png
Properties
CodeSearchAddIn
CodeSearchAddIn.AddIn
CodeSearchAddIn.csproj.user
tags
trunk
BSCodeGrep
bin
Classes
folder_explore copy.ico
Forms
icons
add.png
application_form.png
arrow_right.png
delete.png
find.png
folder.png
folder_explore copy.ico
folder_explore.png
folder_page.png
information.png
lightning.png
magnifier.png
note_edit.png
package_system.png
script_go.png
star.png
Properties
Properties
BSCodeGrep.csproj.user
folder_explore copy.ico
Classes
Forms
icons
add.png
application_form.png
arrow_right.png
BSLogo.jpg
delete.png
exclamation.png
find.png
folder.png
folder_explore copy.ico
folder_explore.png
folder_page.png
information.png
lightning.png
magnifier.png
note_edit.png
package_system.png
page_save.png
script_go.png
star.png
BSCodeGrep.pdb
CodeSearchAddIn.AddIn
CodeSearchAddIn.dll
BSCodeGrep.exe
// 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)

Share

About the Author

thund3rstruck
Software Developer
United States United States
I'm a typical 30 year old generation X guy that likes video games, NFL football, and comic style art. I have an insatiable passion for programming and doing what ever it takes to become a better programmer.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 10 Mar 2009
Article Copyright 2008 by thund3rstruck
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid