I am writing a utility to allow engineers to insert and remove code I have written for one time use. This code will be used to automate one specific (and lengthy) debugging phase, then it will be removed using my utility.

Currently, my utility simply finds the exact function declaration "::OnTestInit( void )\r\n{" (the parent class varies) in a FileStream then inserts a newline and my code. This is far from foolproof as any variation in spacing or parameters would break it (Nobody can think of a reason why the parameters would change, but I do not want to take the chance of it breaking after I'm gone).

I am (obviously) looking for a method that is as close to 100% reliable as possible.

Code is below, here are my thoughts:
Locate the index of an invariate section "OnTestInit" then add its length. Scan from here for the index of the next '(', then ')', then the bracket'{'. If the difference between the indices of the string and the bracket are less than, say 30, characters away and the other strings existed in between you can be fairly sure you did not locate this string in a comment.

This technique is not really much better, though. What if they comment out the whole function and replace it below? I know I've done that.

         * insertCodeButton_Click()
         * When the "Insert Code" button is clicked, this function calls insertCode
         * twice with a different placeholder parameter for each and a different source 
         * file for the data to be inserted for each.
        private void insertCodeButton_Click(object sender, EventArgs e)
            string endPlaceHolder = "::OnTestExit( void )\r\n{";
            string beginPlaceHolder = "::OnTestInit( void )\r\n{";

            //open file
            insertionPath = new StreamReader(this.GetType().Assembly.GetManifestResourceStream(


            //open file
            insertionPath = new StreamReader(this.GetType().Assembly.GetManifestResourceStream(


         * insertCode(string placeHolder)
         * This function will scan for the placeholder value in the code file and insert 
         * the proper data after.  The data resides in a resource file designated by the 
         * static StreamReader insertionPath.
        private void insertCode(string placeHolder) 
            insertionContents = insertionPath.ReadToEnd();  //read contents into variable
            insertionPath.Close();  //close file
            insertionPath.Dispose();  //dispose of variable 

            //verify the presence of the placeholder
            bool isItThere = fileContents.Contains(placeHolder);

            if (isItThere)  //if so, insert the code into proper place
                fileContents = fileContents.Insert(fileContents.IndexOf(placeHolder) +
                    placeHolder.Length, "\r\n\t" + insertionContents + "\r\n\t");
                codeWrite = new StreamWriter(filePath);  //open file to write to
                codeWrite.Write(fileContents);  //overwrite it's entirety
                codeWrite.Close();  //close file
                codeWrite.Dispose();  //dispose of resources
                if (placeHolder.Contains("Init"))
                    MessageBox.Show("OnTestInit not found!");
                if (placeHolder.Contains("Exit"))
                    MessageBox.Show("OnTestExit not found!");

Any thoughts? I would welcome any other criticisms of the code.

7 Years
Discussion Span
Last Post by phillipdaw

Hope I'm not violating etiquette, but anyone have any ideas or links?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.