Monday, December 29, 2014

how to add a method or region in end of a class with parse cs file

On the other hand, if you're writing a quick and dirty utility to update a bunch of your .cs files, then you can do it like this (as long as you realize that all of the projects that have a file that you've modified will need to be recompiled after you're done).



  1. Read the .cs file in using File.ReadLines()

  2. Turn the array into a List and Insert your text

  3. Write the file back using File.WriteAllLines().



string[] lines = File.ReadAllLines(FileName);
List<string> lineList = lines.ToList();

int insertPos = lineList.Count - 2;
lineList.Insert(insertPos++, " #region UniversalLifePrm");
lineList.Insert(insertPos++, " public static T UniversalLifePrm<T>(string configName)");
lineList.Insert(insertPos++, "// Put the rest of your code here.");
lineList.Insert(insertPos, " #endregion");

File.WriteAllLines(FileName, lineList.ToArray());





~~Bonnie DeWitt [C# MVP]


http://geek-goddess-bonnie.blogspot.com


No comments:

Post a Comment