Friday, December 13, 2024

Example of Extension Method & Call CMD No Window

 using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections;
using System.Text.RegularExpressions;

namespace Infor.SyteLine.Search.SearchEngine
{
    /// <summary>
    /// Searcher to search the specified string in file(s).
    /// </summary>
    internal class FileSearcher : ISearchable
    {
        private static readonly string FindStrCommand = @"findstr /S /N ""{0}"" {1} \(-name \*.txt -o -name \*.vb -o -name \*.sql \)";
        private const string Cmd = "cmd.exe";
        private const string LineBreakMark = "\n";
        private const string ExitCommand = "exit";

        /// <summary>
        /// Searches the specified string in file(s).
        /// </summary>
        /// <param name="value"></param>
        public string Search(string value, string directory)
        {
            var process = new Process();
            process.StartInfo.FileName = Cmd;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;

            process.Start();
            process.StandardInput.AutoFlush = true;

            string commandline = string.Format(FindStrCommand, value, directory);
            process.StandardInput.WriteLine(commandline);
            process.StandardInput.WriteLine(ExitCommand);
            string strRst = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            process.Close();
            CloseProcess(Cmd);

            // Remove additional head and foot.
            int contentStartPosition = strRst.IndexOfOccurence(LineBreakMark, 4);
            int lineNum = Regex.Matches(strRst, LineBreakMark).Count;
            int contentEndPosition = strRst.IndexOfOccurence(LineBreakMark, lineNum - 2);

            return strRst.Substring(contentStartPosition, contentEndPosition - contentStartPosition);
        }

        /// <summary>
        /// Closes the process of given name.
        /// </summary>
        /// <param name="processName">to close.</param>
        public void CloseProcess(string processName)
        {
            foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses())
            {
                string tempName = process.ToString();
                int begpos = tempName.IndexOf("(") + 1;
                int endpos = tempName.IndexOf(")");
                tempName = tempName.Substring(begpos, endpos - begpos);
 
                if (tempName == processName)
                {
                    if (!process.CloseMainWindow()) 
                    {
                        process.Kill(); 
                    }                        
                }
            }
        }
    }

    internal static class StringExtensionHelper
    {
        public static int IndexOfOccurence(this string stringToSearch, string match, int occurence)
        {
            int index = 0;
            for (int count = 1; count <= occurence && (index = stringToSearch.IndexOf(match, index + 1)) != -1; count++)
            {
                if (count == occurence)
                    return index;
            }

            return -1;
        }
    }
}

No comments:

Post a Comment